场景
Android系统8.0及以上,开启Service必须使用startForegroundService(Intent intent)方法,对应的Service则必须设置startForeground(int id, Notification notification),因而我们必须创建一个通知,这样在服务运行当中,会一直显示一条“xx正在运行”类似的通知。

目标
消除“xx正在运行”的通知内容

解决方案
大体思路是:一个最后保留的MyService,一个辅助消除通知的MyService1, 利用 MyService去绑定MyService1,startForeground变为前台服务,注意一定要使用一样的Notification ID,然后MyService1取消前台效果stopForeground从而删除通知。

  1. AndroidManifest 添加权限
  <!--service前台服务--><uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  1. 业务服务
package com.example.demo;import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;import androidx.annotation.RequiresApi;/*** @author: ding xujun* @created on: 2020/12/10 23:01* @project: demo*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public class Myservice extends Service {@Overridepublic IBinder onBind(Intent intent) {return null;}@RequiresApi(api = Build.VERSION_CODES.O)@Overridepublic void onCreate() {super.onCreate();Log.d("Myservice=======", "onCreate");if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {startForeground(ServiceNotificationManager.NOTIFICATION_FOREGROUND_ID, ServiceNotificationManager.getNotification(this));Intent intent = new Intent(this, Myservice1.class);startForegroundService(intent);}}@RequiresApi(api = Build.VERSION_CODES.O)@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d("Myservice=======", "onStartCommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.d("Myservice=======", "onDestroy");}
}
  1. 辅助服务消除通知服务
package com.example.demo;import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;/*** @author: ding xujun* @created on: 2020/12/13 11:54* @project: demo*/
public class Myservice1 extends Service {@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();startForeground(ServiceNotificationManager.NOTIFICATION_FOREGROUND_ID, ServiceNotificationManager.getNotification(this));Log.d("Myservice=======", "onCreate1");stopSelf();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d("Myservice=======", "onStartCommand1");return super.onStartCommand(intent, flags, startId);}@RequiresApi(api = Build.VERSION_CODES.O)@Overridepublic void onDestroy() {super.onDestroy();//stopForeground 停止服务并且取消通知栏 ,这种情况比较特殊,// 所以还要调用ServiceNotificationManager.removeNotification();ServiceNotificationManager.removeNotification();stopForeground(true);Log.d("Myservice=======", "onDestroy1");}}
  1. 辅助工具类
package com.example.demo;import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;import androidx.annotation.RequiresApi;/*** @author: ding xujun* @created on: 2020/12/13 11:56* @project: demo*/
public class ServiceNotificationManager {public static Notification mNotification = null;private static NotificationChannel mChannel = null;private static NotificationManager mNotificationManager;private static final String NOTIFICATION_CHANNEL_ID = "1";public static final int NOTIFICATION_FOREGROUND_ID = 1;public static Notification getNotification(Context context) {if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {if (mNotificationManager == null) {mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);}if (mChannel == null) {mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, context.getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);}mNotificationManager.createNotificationChannel(mChannel);if (mNotification == null) {mNotification = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID).build();}}return mNotification;}@RequiresApi(api = Build.VERSION_CODES.O)public static void removeNotification() {if (mNotificationManager == null) {return;}mNotificationManager.cancel(NOTIFICATION_FOREGROUND_ID);mNotificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID);}
}
  1. 开启服务
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {startForegroundService(intent);} else {startService(intent);}

目前华为手机测试可以,其他版本手机待测试,后续补充版本兼容测试

借鉴
android 8.0 以上 开启Service: IllegalStateException: Not allowed to start service Intent … 异常

Android 8.0 极光推送通知栏显示 ’xxx正在运行‘相关推荐

  1. android 8.0 nexus7,Android 7.0即将推送 Nexus用户最迟8月22日更新

    原标题:Android 7.0即将推送 Nexus用户最迟8月22日更新 [丫丫网]Android N终于来了.今年5月,谷歌正式发布最新的Android N系统.7月1日,谷歌都会在位于加州山景城的 ...

  2. 关于极光推送通知栏无法正确显示应用图标

    今天在集成极光推送的时候遇到了一个很无奈的问题,在推送时应用的通知图标一直显示极光的图标,折腾了许久也没折腾出来. 我先是将demo的包名改成自己的应用包名跑了一次,效果还行,然后就根据文档将推送sd ...

  3. Android之集成极光推送消息

    极光推送是为 App 提供第三方推送服务的平台之一,它提供四种消息形式: 通知 自定义消息 富媒体 本地通知 1.通知 Push Notification,即指在手机的通知栏(状态栏)上会显示的一条通 ...

  4. 极光推送 android6.0,极光推送Unity-Android-API

    Android API 停止与恢复推送服务 清除通知 设置允许推送时间 设置通知静默时间 申请权限接口(用于 Android 6.0 及以上系统) 通知栏样式定制 设置保留最近通知条数 本地通知 获取 ...

  5. 极光推送 android 最新,Android——快速集成极光推送-Go语言中文社区

    集成极光推送 1,首先肯定是注册,添加应用 2,开始自动集成比手动集成简单第一步 在 build.gradle defaultConfig { multiDexEnabledtrue applicat ...

  6. .net平台借助第三方推送服务在推送Android消息(极光推送) 转

    分类: .net外部工具接口(3) .net知识精华(29) 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近做的.net项目(Windows Service)需要向Android手机发送推 ...

  7. Android之集成极光推送

    安卓端集成极光推送是很常见的,极光推送的简单高效性适合很多想要集成推送的APP,如果你要自己装逼,也可以自己写推送,只要想做都是可以的. 第一步.Gradle配置(Module的build.gradl ...

  8. 更改极光推送通知栏图标

    //更改极光推送图标 private void initChangeJPushLogo() {BasicPushNotificationBuilder builder = new BasicPushN ...

  9. android 9 pie公司,Android 9.0正式推送 定名Android Pie

    [IT168 手机讯]北京时间8月7日早间,谷歌率先面向Pixel系列手机推送了Android P正式版(Essential也第一时间升级),代号"Pie",这也是Android的 ...

最新文章

  1. SAP WM 确认TO单的时候不能修改目的地STORAGE BIN
  2. linux c send recv MSG_NOSIGNAL参数
  3. java 判断二叉树是否平衡_剑指Offer - 判断二叉树是否是平衡二叉树
  4. 像证券交易员一样思考和行动_3纪律与心态
  5. Java Character 类
  6. css3动画--位移加阴影
  7. [USACO13JAN] Cow Lineup (单调队列,尺取法)
  8. 一号信令是什么?1号信令和7号信令的区别介绍!
  9. 基于JDBC的数据库连接池技术研究与应用
  10. 【年度重磅】《2021营销自动化应用基准报告》正式发布!
  11. 虚拟机服务器实验三十一 Windows Server 2012 RDS桌面虚拟化之二VDI标准部署之托管共享桌面虚拟化...
  12. ASP.NET ActionPack
  13. Overloud Gem Modula for Mac - 三合一经典调制插件
  14. 拓端tecdat|R语言相关分析和稳健线性回归分析
  15. html把毫秒转换成年月日,JS实现获取毫秒值及转换成年月日时分秒的方法
  16. 洛谷 P1573 栈的操作
  17. CMOS模拟电路设计经典书籍介绍 ---看完这些模电书,那离大佬就不远了
  18. 【Android】之 App Startup 的使用及浅析
  19. pe里怎么看不到计算机硬盘,进去pe系统找不到硬盘怎么回事_进入pe系统看不到硬盘如何解决-win7之家...
  20. xmarks不能同步和访问之解决办法

热门文章

  1. windows cuda更新过程
  2. Java 实现小游戏双人匹配机制
  3. Extjs常用属性、方法
  4. centos 7,局域网不能访问禅道
  5. opencv十四天入门学习——task2
  6. ASO优化:需要注意的4点错误事项
  7. 连发消息小代码(vbs)
  8. 一个快速打开微信收藏的方法
  9. PHP设计模式之装饰器模式
  10. 【Android安全】安卓app 防篡改方案总结