【问题标题】:Notification status bar text通知状态栏文字
【发布时间】:2015-04-21 03:25:40
【问题描述】:

我想实现一个通知,在 android 的状态栏中显示互联网速度,我还希望这个通知不能被用户删除,只能被应用程序本身删除。
我查看了NotificationCompat.Builder Api,但找不到任何用于在状态栏中设置文本并定期更新的 Api。
我知道可以实现此功能,但我不知道如何实现它。
我找到了一个很好地实现它的应用程序,它的名字是internet speed meter lite
如您所知,setSmallIconNotificationCompat.Builder 无法实现此功能。
我放了图片以便更好地理解。
android状态栏中的网速:
图1

图2

用户无法移除的通知:
图片 3

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

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

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

【问题讨论】:

    标签: android android-notifications


    【解决方案1】:

    试试这个..

    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。
    • 另外我应该说我在通知中测试了代码文本,但它没有按我想要的那样操作。
    【解决方案2】:

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

    有一个 notify 方法,您可以在每次需要时调用它,例如从服务中调用它,它将寻找您的连接并告诉如何更改它。

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

    【讨论】:

    • 我的意思是通知状态栏文本,如附加到问题而不是通知文本的图像中所示。换句话说,我想在状态栏中显示文本而不是通知图标。此文本将显示互联网速度。请查看有问题的图片 1 和 2。
    • @taher 也许这会有所帮助? stackoverflow.com/questions/19610598/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多