简体   繁体   English

向服务添加startForeground()时,将创建两个通知,而不是一个通知

[英]While adding startForeground() to a service two notifications are created instead of one

So I've created a service and it works great. 因此,我创建了一项服务,并且效果很好。 All it does is just count. 它所做的只是计数。 I have a custom class that handles the hassle of using notifications. 我有一个自定义类,处理使用通知的麻烦。 This class has a getNotification which, obviously, returns the notification it uses. 此类有一个getNotification,显然,它返回其使用的通知。 Everything worked great but I've got to make my service to run on foreground (it syncs some important data to the app which must not be interrupted until it finishes) Right now when I'm adding startForeground I add my notification and an id which leaves it like this. 一切都很好,但是我必须使我的服务在前台运行(它将一些重要数据同步到应用程序,在完成之前不得中断)现在,当我添加startForeground时,我添加了通知和一个ID像这样离开它。 startForeground(1337, notification); startForeground(1337,通知);

Problem I have is that the first notify() I do is, for some reason, independent from the others notifications. 我遇到的问题是,由于某种原因,我执行的第一个notify()与其他通知无关。 So when I make this run it creates two notifications. 因此,当我进行此运行时,它将创建两个通知。 The first one is stuck on the first update which has a title called "Zilean" and it's content says "Counting". 第一个卡在第一个更新中,该更新的标题为“ Zilean”,内容为“ Counting”。 The other one updates perfectly. 另一个更新完美。 I've notice that if startForeground is ran with an id of 0 (startForeground(0,notification)) then this problem gets fixed, but if I kill the activity the notification dies. 我注意到,如果以id为0(startForeground(0,notification))运行了startForeground,则此问题得到解决,但是如果我终止了该活动,则通知将死亡。 Doesn't happen when the id <> 0. 当id <> 0时不会发生。

I had this problem for too long so I'm using a dummy service which only counts. 我遇到这个问题的时间太长了,因此我正在使用仅计数的虚拟服务。 I want to know that if the activity dies due to the user or just because android decided to delete it's memories, then the service will keep going. 我想知道,如果活动是由于用户而死,或者仅仅是因为android决定删除它的记忆,那么该服务将继续下去。

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

So.. what I'm missing? 所以..我想念的是什么?

Solved, my error was that the notification id wasnt the same as the startForeground id I was passing (1337) 解决了,我的错误是通知ID与我传递的startForeground ID不相同(1337)

Edit: It's important to note that the notification id and the service id must not be 0, else it'll blend with the main activity thread 编辑:请注意,通知ID和服务ID不得为0,否则它将与主活动线程混合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM