首页 > 如何移除前台的服务的通知。

如何移除前台的服务的通知。

servicenotifaction绑定在一起的时候notifaction是无法移除的,我google了一下发现更新builder.setongoing(false)就可以移除了,但是我自己写了简单的代码发现并没有起作用。希望可以指出那里出现了问题。
activity

 Intent i = new Intent(this,MyService.class);
 startService(i);

service

if (intent.getAction()==null){
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this).
                    setContentTitle("wo")
                    .setContentText("n")
                    .setOngoing(true)
                    .setSmallIcon(R.mipmap.ic_launcher);
            Intent i  = new Intent(this,MyService.class);
            i.setAction(ACTION);
            PendingIntent pi = PendingIntent.getService(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(pi);
            startForeground(100,builder.build());
        }else if (intent.getAction().equals(ACTION)){
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this).
                    setContentTitle("wo")
                    .setContentText("n")
                    .setOngoing(false)
                    .setSmallIcon(R.mipmap.ic_launcher);
            startForeground(100,builder.build());
        }
        return super.onStartCommand(intent, flags, startId);
    }

请问那里出现了问题(点击notifaction更新通知,想要实现类似于google music的通知效果),请问那里出现了问题码,谢谢?


你两个地方发出的通知的内容相同,可能就是这样你看不出通知被更新了。
另外,你到底是要看通知更新了吗,还是要彻底移除通知,要彻底移除通知的话就要调用 stopForeground();方法了。
builder.setongoing(false)这个方法只是将通知设置成非Onging状态,不能移除通知。

【热门文章】
【热门文章】