Android 将startForeground()添加到服务时,将创建两个通知,而不是一个通知
因此,我创建了一个服务,效果非常好。它所做的只是计数。我有一个自定义类来处理使用通知的麻烦。这个类有一个getNotification,它显然返回它使用的通知。一切都很好,但我必须让我的服务在前台运行(它将一些重要数据同步到应用程序,在它完成之前不得中断)。现在,当我添加startForeground时,我添加了我的通知和一个id,这样就可以了。 startForeground(1337,通知) 我的问题是,出于某种原因,我做的第一个notify()独立于其他通知。因此,当我运行此程序时,它会创建两个通知。第一次更新的标题是“Zilean”,内容是“Counting”。另一个完全更新。我注意到,如果startForeground运行时id为0(startForeground(0,notification)),则此问题会得到修复,但如果我终止活动,则通知将终止。id为0时不会发生 我有这个问题的时间太长了,所以我使用了一个虚拟服务,它只起作用。我想知道,如果该活动因用户原因或仅仅因为android决定删除其内存而终止,那么该服务将继续运行Android 将startForeground()添加到服务时,将创建两个通知,而不是一个通知,android,service,notifications,Android,Service,Notifications,因此,我创建了一个服务,效果非常好。它所做的只是计数。我有一个自定义类来处理使用通知的麻烦。这个类有一个getNotification,它显然返回它使用的通知。一切都很好,但我必须让我的服务在前台运行(它将一些重要数据同步到应用程序,在它完成之前不得中断)。现在,当我添加startForeground时,我添加了我的通知和一个id,这样就可以了。 startForeground(1337,通知) 我的问题是,出于某种原因,我做的第一个notify()独立于其他通知。因此,当我运行此程序时,它会创
// onStartCommand
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// NOTIFICATION SECTION
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this);
notificationManagerClass = new SBNNotification(mNotifyManager,
mBuilder, true, 1, false, "Zilean",
"Initiating Counter", false);
notificationManagerClass.notificate();
final Notification notification = notificationManagerClass.getNotification();
notification.flags = flags;
notification.defaults = Notification.DEFAULT_LIGHTS;
startForeground(1337, notification);
new Timer().scheduleAtFixedRate(new TimerTask () {
int i=0;
@Override
public void run() {
notificationManagerClass.setContentTitle("Contando");
notificationManagerClass.setContentText(String.valueOf(i));
notificationManagerClass.notificate();
i++;
Log.e("HOLAAA", String.valueOf(i));
}}, 0, 1000);
return START_REDELIVER_INTENT;
}
所以。。我遗漏了什么?解决了,我的错误是通知id与我传递的startForeground id不一样(1337)
编辑:需要注意的是,通知id和服务id不能为0,否则它将与主活动线程混合解决,我的错误是通知id与我传递的startForeground id不同(1337) 编辑:请务必注意,通知id和服务id不能为0,否则它将与主活动线程混合