我是靠谱客的博主 刻苦小丸子,最近开发中收集的这篇文章主要介绍android 清除通知栏,android startForeground去除通知栏,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

首先写一个BootstartService,顾名思义,这个service只是起引导作用,干完活就退出了。最精华的部分其实就是这句stopSelf(),说白了这个service其实还没起起来就被停掉了,这样onDestroy()里就会调用stopForeground(),通知栏的常驻通知就会被消掉。

public class BootstartService extends Service {

@Override

public void onCreate() {

super.onCreate();

startForeground(this);

// stop self to clear the notification

stopSelf();

}

@Override

public void onDestroy() {

super.onDestroy();

stopForeground(true);

}

public static void startForeground(Service context) {

context.startForeground(8888, new Notification());

}

}

接下来写我们的主service,主service会先调用一次startForeground(),然后再启动BootstartService。

public class MainService extends Service {

@Override

public void onCreate() {

super.onCreate();

BootstrapService.startForeground(this);

// start BootstartService to remove notification

Intent intent = new Intent(this, BootstartService.class);

startService(intent);

}

@Override

public void onDestroy() {

super.onDestroy();

stopForeground(true);

}

}

看到这里大家应该已经明白了,说白了就是两个service共用一个notification ID,第一个service起来的时候会显示通知栏,然后第二个service停掉的时候去除通知栏。

最后

以上就是刻苦小丸子为你收集整理的android 清除通知栏,android startForeground去除通知栏的全部内容,希望文章能够帮你解决android 清除通知栏,android startForeground去除通知栏所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部