通知状态栏文本

4
我希望在Android状态栏中实现一个显示网络速度的通知,而且希望这个通知不可由用户移除,只能由应用程序本身移除。
我查看了NotificationCompat.Builder API,但是无法找到设置状态栏文本并实时更新的相关API。
我知道实现此功能是可能的,但我不知道如何实现它。
我找到了一个很好地实现了这一功能的应用程序,它的名字叫做internet speed meter lite。
正如您所知,通过NotificationCompat.BuildersetSmallIcon无法实现此功能。
我放了一些图片以便更好地理解。
Android状态栏中的网络速度:
Image 1
Internet speed in status bar of android![][1]
Image 2
enter image description here
不可被用户移除的通知:
Image 3
Notification that is not removable by user

更新:
这是我的通知代码,但它没有按照我的期望执行。
我在通知中使用滚动文本来向用户显示速度,但它没有按照我的期望执行。

public class DownloadSpeedNotification {

private NotificationCompat.Builder mBuilder;
private Context mContext;
private static final int NOTIFICATION_ID = 2;

public DownloadSpeedNotification(Context context) {
    mContext = context;
    mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.notification_icon).
                    setContentText("Download meter content text").
                    setContentTitle("Download meter content title");

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);
}

public void alert() {
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    // Builds the notification and issues it.
    mNotifyMgr.notify(NOTIFICATION_ID, mBuilder.build());
}

public void setSpeed(String speed) {
    mBuilder.setTicker(speed);
}
}

以下是使用上述类通知用户的代码示例:
downloadSpeedNotification.setSpeed(Formatter.humanReadableByteCount(rx, true));
        downloadSpeedNotification.alert();

每秒钟调用以上代码。
2个回答

0

试一试这个。。

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(ns);
long when = System.currentTimeMillis();

Notification notification = new Notification("your icon", "your ticker text", when);
/*<set your intents here>*/
mNotificationManager.notify(notificationID, notification);

notificationID替换为整数。只要您发送具有相同notificationID的新通知,它就会替换旧通知。


我的意思是通知状态栏文本,如问题附带的图像所示,而不是通知文本。换句话说,我想在状态栏中显示文本,而不是通知图标。这个文本将显示互联网速度。请查看问题中的图像1和2。 - taher
另外我应该说,我在我的通知中测试了滚动文本,但它没有按照我想要的方式执行。 - taher

0

我的意思是通知状态栏文本,如问题附带的图像所示,而不是通知文本。换句话说,我想在状态栏中显示文本,而不是通知图标。这个文本将显示互联网速度。请查看问题中的图像1和2。 - taher
@taher也许这个会有帮助? http://stackoverflow.com/questions/19610598/android-adding-text-from-textview-to-status-bar - k.nadonenko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接