通知状态栏文本

时间:2015-04-20 15:32:16

标签: android android-notifications

我想在android的状态栏中实现一个显示互联网速度的通知我也希望这个通知不能由用户删除,只能由应用程序本身删除。
我查看了NotificationCompat.Builder Api,但我找不到任何Api在状态栏中设置定期更新的文字。
我知道可以实现这个功能,但我不知道如何实现它 我找到了一个很好地实现它的应用程序,它的名字是internet speed meter lite 如您所知,setSmallIcon的{​​{1}}无法实现此功能 为了更好地理解,我把图像放在了一起 android状态栏中的网速:
图片1
Internet speed in status bar of android![][1]
图片2
enter image description here
用户无法删除的通知:
图片3
Notification that is not removable by user

更新
这是我的通知代码,但它没有按我的意愿行动 我在通知中使用了ticker文本向用户显示速度,但它没有按我的意愿动作。

NotificationCompat.Builder

这是使用上面的类来通知用户的代码:

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);
}
}

以上代码每1秒调用一次。

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用NotificationManager? http://developer.android.com/reference/android/app/NotificationManager.html

这是一种通知方法,您可以在需要时从服务中每次调用它,这将寻找您的连接并告诉如何更改它。

编辑1 Google甚至提供了一个很好的例子,如何使用通知管理器 http://developer.android.com/training/notify-user/managing.html

答案 1 :(得分: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发送新通知,它就会替换旧通知。

相关问题