2014-09-26 23 views
0

正如Richard在这个问题中所说>Possible to continuously update text in the notification area?,我在这里有同样的问题。
我想更新通知区域中的文本。
这个问题被回答和接受,但答案不帮助我。
即将动态创建文本为位图,并将其设置为通知的小图标。
但正如Richard评论的那样,setSmallIcon的图像必须在软件包中进行预定义。无法在运行中编辑它们。Android:不断更新通知区域中的文本

请让我看看这个东西的正确方法。

回答

0

更新您的通知非常简单。 首先,当你创建它时,你必须使用一个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的新的通知,这将被更新(不需要重置你不想更新paraneter)。

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

谢谢您的回答,我已经与此代码完成。检查这个链接>>(http://developer.android.com/guide/topics/ui/notifiers/notifications.html)但它只是更新'Notification Drawer'的内容文本,我想要的是在'通知区域“并持续更新此文本。注意 - 要在“通知区域”中更新,而不是在“通知抽屉”中更新。 – zey 2014-09-29 18:14:48

+0

好吧,但要做到这一点,你必须dinamically与您的文本创建一个图标,然后更新它是实现你想要的东西的唯一方法... – 2014-09-29 18:20:05

+0

正如我在我的问题中提到'setSmallIcon的图像必须在包,没有能力在飞行中编辑它们。不是吗?或者,有没有办法做到这一点? – zey 2014-09-29 18:22:12

0

我试图让一个,但它有一个缺点是,它在通知抽屉一个空的通知,

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