【问题标题】:Android: continuously update text in the notification areaAndroid:不断更新通知区域中的文字
【发布时间】:2014-10-05 15:25:51
【问题描述】:

正如理查德在这个问题中所说的那样 > Possible to continuously update text in the notification area? ,我在这里遇到了同样的问题。
我想更新通知区域中的文本。
这个问题得到了回答和接受,但答案对我没有帮助。
即将动态创建文本为位图并将其设置为通知的小图标。
但正如理查德评论的那样,setSmallIcon 的图像必须在包中预定义。无法即时编辑它们。

请告诉我做这件事的正确方法。

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    更新通知非常简单。 首先,当你创建它时,你必须用一个 id 创建它。

    NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // Sets an ID for the notification, so it can be updated
    int notifyID = 1;
    mNotifyBuilder = new NotificationCompat.Builder(this)
        .setContentTitle("Title")
        .setContentText("Text")
        .setSmallIcon(R.drawable.ic_notify);
    mNotificationManager.notify(
                notifyID,
                mNotifyBuilder.build());
    

    现在要更新您的通知,您只需要使用旧 id 通知新通知,这将被更新(您不需要重置您不想更新的参数)。

      mNotifyBuilder.setContentText(setContentText("New Text")
      mNotifyBuilder.setSmallIcon(R.drawable.ic_new_notify);
        //this update your notification
        mNotificationManager.notify(
                    notifyID,
                    mNotifyBuilder.build());
    

    【讨论】:

    • 感谢您的回答,我已经完成了这段代码。检查此链接>>(developer.android.com/guide/topics/ui/notifiers/…)但它只是更新Notification Drawer的内容文本,我想要在Notification Area中显示文本并不断更新此文本。注意 - 在 Notification Area 中更新,而不是在 Notification Drawer 中。
    • 好的,但要做到这一点,您必须使用文本动态创建一个图标,然后对其进行更新,这是实现您想要的唯一方法...
    • 正如我在问题the images for setSmallIcon must be predefined in the package.There is no ability to edit them on the fly 中提到的。不是吗?或者,有什么办法吗?
    • 您可以在此处以示例 [链接] (snipplr.com/view/73714/…) 动态创建带有文本的位图。或者您可以使用此方法 [link] (stackoverflow.com/questions/4688052/…) 来实现您想要的效果
    • 对不起,我想为我的错误道歉,但 setSmallIcon(int drawableid) 方法仅适用于现有的可绘制资源,如果您使用位图,您必须使用 setLargeIcon(Bitmap icon) 但如果不犯另一个错误(现在我要检查),此图标显示在通知抽屉中,而不是在通知区域中
    【解决方案2】:

    我试着做一个,但它有一个缺点是,它在通知抽屉里有一个空通知,

    public static int when = 0;
    private void generateNotification(Context context) {
        Log.i(TAG, "generateNotification");
        Intent notificationIntent = new Intent(context, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(
                this.getApplicationContext(), 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    
        NotificationCompat.Builder mNotification = new NotificationCompat.Builder(
                this)
                // .setContentTitle("AppName")
                // .setContentText("Message For Notification Drawer")
                // .setSound(soundUri)
                // .setDefaults(Notification.DEFAULT_SOUND)
                // .setVibrate(new long[] { 1000, 1000 })
                // .addAction(R.drawable.ic_launcher, "View", pIntent)
                // .addAction(0, "Remind", pIntent)
                // .setNumber(fCount)
                // .setWhen(when)
                .setSmallIcon(R.drawable.ic_launcher)
                .setTicker(Integer.toString(when)) // Set String you want
                .setAutoCancel(true)
                .setContentIntent(pIntent);
    
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
        // notificationManager.notify(when, mNotification.build());
        notificationManager.notify(1, mNotification.build());
        when++;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多