Android的通知经理更新率 [英] Android notification manager update percentage

查看:135
本文介绍了Android的通知经理更新率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用通知管理器进行内容下载,并显示下载完成的百分比,但每次我打电话了新百分之displaymessage功能它创造了新的通知。如何我只是没有每次都创建新的更新通知?

 公共无效displaymessage(字符串字符串){
字符串NS = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =(NotificationManager)getSystemService(NS);
INT图标= R.drawable.icon;
CharSequence的tickerText =沙米尔下载服务;
时长= System.currentTimeMillis的();
通知通知=新的通知(图标,tickerText时);
上下文的背景下= getApplicationContext();
CharSequence的contentTitle =下载内容;
notification.setLatestEventInfo(上下文,contentTitle,字符串空);
最终诠释HELLO_ID = 2;
mNotificationManager.notify(HELLO_ID,通知);}


解决方案

我所做的就是存放在类级别的变量通知什么..
也许改变你的功能createDownloadNotification并使用你所拥有的上面,除了化妆通知变量访问到整个类。

然后有另一个功能(类似于updateDownloadNotification),这将调用setLatestEventInfo与更新信息的通知。

另外请注意,你需要调用mNotificationManager.notify(HELLO_ID,通知);之后每次更新或什么都不会改变。

--- ---更新
其实你可以只是1功能,并检查是否通知为null(如果没有,创建一个)以其他方式使用你已经拥有的。

例如:

 公共类YourClass延伸服务{//否则可能延长活动民营通知mNotification = NULL;公共无效displaymessage(字符串字符串){
  字符串NS = Context.NOTIFICATION_SERVICE;  NotificationManager mNotificationManager =(NotificationManager)getSystemService(NS);
  INT图标= R.drawable.icon;
  CharSequence的tickerText =沙米尔下载服务;
  时长= System.currentTimeMillis的();
  如果(mNotification == NULL){
    mNotification =新的通知(图标,tickerText时);
  }
 //mNotification.when =当; //不知道你是否需要更新此尝试这两种方法
  上下文的背景下= getApplicationContext();
  CharSequence的contentTitle =下载内容;
  notification.setLatestEventInfo(上下文,contentTitle,字符串空);
  最终诠释HELLO_ID = 2;
  mNotificationManager.notify(HELLO_ID,mNotification);}

我的code实际上是所有的有点不同,我更新是通知的iconLevel每次更新,所以我不知道,如果每次改变,如果你需要更新mNotification.when

试试看并报告。

我也会让一些变量这一职能的。通常你,如果它是类的私有实例变量名的变量mSomething。以下是我建议:

 私人通知mNotification = NULL;
私人NotificationManager mNotificationManager = NULL;
私有静态最终诠释HELLO_ID = 2;公共无效displaymessage(字符串字符串){
  字符串NS = Context.NOTIFICATION_SERVICE;
  如果(mNotificationmanager == NULL){
      mNotificationManager =(NotificationManager)getSystemService(NS);
  }
  INT图标= R.drawable.icon;
  CharSequence的tickerText =沙米尔下载服务;
  时长= System.currentTimeMillis的();
  如果(mNotification == NULL){
    mNotification =新的通知(图标,tickerText时);
  }
  //mNotification.when =当; //不知道你是否需要更新此尝试这两种方法
  上下文的背景下= getApplicationContext();
  notification.setLatestEventInfo(上下文,contentTitle,字符串空);
  mNotificationManager.notify(HELLO_ID,mNotification);}

I am using Notification Manager for content downloading and show the percent of completed download, but each time i call displaymessage function with new percent it creates new notification. How can i just update the notification without creating new one each time?

public void displaymessage(String string) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Shamir Download Service";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Downloading Content:";
notification.setLatestEventInfo(context, contentTitle, string, null);
final int HELLO_ID = 2;
mNotificationManager.notify(HELLO_ID, notification);

}

解决方案

What I did was store the notification in a class level variable.. Maybe change your function to createDownloadNotification and use what you have above, except make notification a variable accessible to the whole class.

Then have another function (something like updateDownloadNotification) which will call setLatestEventInfo on the notification with the updated information.

Also note that you need to call mNotificationManager.notify(HELLO_ID, notification); after each update or nothing will change.

--- Update --- Actually you could just have 1 function and check if notification is null (if not,create it) otherwise use what you already have.

Example:

public class YourClass extends Service { //or it may extend Activity

private Notification mNotification = null;

public void displaymessage(String string) {
  String ns = Context.NOTIFICATION_SERVICE;

  NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  int icon = R.drawable.icon;
  CharSequence tickerText = "Shamir Download Service";
  long when = System.currentTimeMillis();
  if (mNotification == null) {
    mNotification = new Notification(icon, tickerText, when);
  }
 //mNotification.when = when;//not sure if you need to update this try it both ways
  Context context = getApplicationContext();
  CharSequence contentTitle = "Downloading Content:";
  notification.setLatestEventInfo(context, contentTitle, string, null);
  final int HELLO_ID = 2;
  mNotificationManager.notify(HELLO_ID, mNotification);

}

My code is actually a bit different in that all I'm updating is the notification's iconLevel on each update, so I'm not sure if with each change if you need to update mNotification.when

Try it and see and report back.

Also I would make some variables out of this function.. Usually you name a variable mSomething if it is a private instance variable of the class. Here is what I would recommend:

private Notification mNotification = null;
private NotificationManager mNotificationManager = null;
private static final int HELLO_ID = 2;

public void displaymessage(String string) {
  String ns = Context.NOTIFICATION_SERVICE;
  if (mNotificationmanager == null) {
      mNotificationManager = (NotificationManager) getSystemService(ns);
  }
  int icon = R.drawable.icon;
  CharSequence tickerText = "Shamir Download Service";
  long when = System.currentTimeMillis();
  if (mNotification == null) {
    mNotification = new Notification(icon, tickerText, when);
  }
  //mNotification.when = when;//not sure if you need to update this try it both ways
  Context context = getApplicationContext();      
  notification.setLatestEventInfo(context, contentTitle, string, null);      
  mNotificationManager.notify(HELLO_ID, mNotification);

}

这篇关于Android的通知经理更新率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆