android o android8.0 startforegroundservice startforegroundservice() did not then call service.star
解决context.startforegroundservice() did not then call service.startforeground()
原因:
- Android 8.0 系统不允许后台应用创建后台服务,故只能使用
Context.startForegroundService()
启动服务 - 创建服务后,应用必须在5秒内调用该服务的
startForeground()
显示一条可见通知,声明有服务在挂着,不然系统会停止服务 + ANR 套餐送上。 - Notification 要加 Channel,系统的要求。
解决步骤:
Service.java 中
@Overridepublic void onCreate() {super.onCreate();if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {startFG();}}private void startFG() {NotificationBuilder builder = new NotificationBuilder(this);final Notification notification = builder.buildNotification();startForeground(NotificationBuilder.NOTIFICATION_ID, notification);}
NotificationBuilder.java
public final class NotificationBuilder {public static final String NOTIFICATION_CHANNEL_ID = "com.demo.CHANNEL_ID";public static final int NOTIFICATION_ID = 0xD660;private final Context mContext;private final NotificationManager mNotificationManager;public NotificationBuilder(Context context) {mContext = context;mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);}public Notification buildNotification() {if (shouldCreateNowPlayingChannel()) {createNowPlayingChannel();}NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);return builder.setSmallIcon(R.drawable.all_app_takeaway_icon).setContentTitle("").setContentTitle("").setOnlyAlertOnce(true).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).build();}@RequiresApi(api = Build.VERSION_CODES.O)private boolean nowPlayingChannelExists() {return mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) != null;}private boolean shouldCreateNowPlayingChannel() {return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !nowPlayingChannelExists();}@RequiresApi(api = Build.VERSION_CODES.O)private void createNowPlayingChannel() {final NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"当前播放",NotificationManager.IMPORTANCE_LOW);channel.setDescription("当前播放的电台");mNotificationManager.createNotificationChannel(channel);}
}
还要注意:
AndroidManifest.xml 中配置permission
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
启动Service方法:
//后台启动serviceif (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {Intent serviceIntent = new Intent(this, DemoService.class);startForegroundService(serviceIntent);} else {Intent serviceIntent = new Intent(this, DemoService.class);startService(serviceIntent);}
android o android8.0 startforegroundservice startforegroundservice() did not then call service.star相关推荐
- 华为android贡献度,Android8.0系统占比大幅提升,华为、荣耀、小米成为主要贡献...
原标题:Android8.0系统占比大幅提升,华为.荣耀.小米成为主要贡献 安卓推出Android8.0系统至今已半年有余,如今Android8.1稳定版.Android9.0开发者预览版也发布了,但 ...
- 支付宝Android接口4.0以上报错Failure calling remote service
很坑爹的问题,4.0一下没问题,完全按照接口文件写的.网上一查,很多人遇到.最好直接在4.2下调试,看看报错问题,然后度娘. 将RSA文件中的加上"BC"后测试通过...NND P ...
- android魅族升级,Android8.0快来了,魅族手机终于要升级 7.0了!
原标题:Android8.0快来了,魅族手机终于要升级 7.0了! 谷歌在今年5月份的I/O2017开发者大会上发布了第二个Android O(Android8.0)开发者预览版,相信再过几个月And ...
- Android 8.0 学习(14)---Android8.0适配分析
公司项目Android8.0适配分析 1. 官方推荐的兼容测试方式 2. Android8.0行为变更说明 Android 8.0 行为变更包括两个部分:针对所有 API 级别的应用和针对 And ...
- android 打印kernel log,android8.0 kernel4.9.44 各层log打开
一.问题 平台android8.0 kernel4.9.44,想加个i2c驱动,打开log都是一件脑瓜疼的事. 二.log总结 android有三种log. 1.kernel下面的,就是driver层 ...
- android 跳转动画黑屏,Android8.0 适配解决页面跳转过程出现短暂黑屏的问题
小菜在适配 Android8.0 过程中,遇到很多问题,有很多很常见的问题,今天来整理一下页面跳转时黑屏的问题. 问题猜测 猜测一: 显示 Activity 页面之前会优先开启一个 Starting ...
- Android 8.0 (35)----Android8.0.0-r4的OTA升级流程
Android8.0.0-r4的OTA升级流程 原网址:https://blog.csdn.net/dingfengnupt88/article/details/52875228 Android系统 ...
- Android 8.0 学习(17)---Android8.0中对指纹的新要求
Android8.0中对指纹的新要求 784 我们先来看一张指纹在Android6.0版本上的架构层次图: 指纹应用层,也就是手机上的指纹设置,这是Android系统层定义的指纹管理入口. 1,sys ...
- Android 8.0 学习(24)---Android8.0 WiFi热点适配
Android8.0 WiFi热点适配 在Android8.0上用以前的方式调试WiFi热点的时候发现无法正常开启热点,于是查了下,发现之前的热点打开接口已经废弃.原先的接口WifiManager.j ...
最新文章
- 抗击疫情!阿里云为加速新药疫苗研发提供免费AI算力
- 初学者如何选出最适合自己深度学习框架?
- c语言调用shell命令一 popen使用以及获取命令返回值
- c语言复制后无法运行,刚学C语言,在Linux下写的代码能正常编译,复制到VC下就无法运行...
- JSP第十四次课:JSP项目开发高级操作2---在线编辑器应用及前台首页显示商品
- del服务器如何收系统日志,利用Rsyslog集中收集系统日志和用户操作记录以及相关处理方法...
- 只安装oracle服务端连plsql,Oracle 不安装Oracle客户端,使用PLSQL连接Oracle服务器
- Android开发笔记(一百三十七)自定义行为Behavior
- POJ 1151 Atlantis 矩形面积求交/线段树扫描线
- MySQL -- 行转列 -- GROUP_CONCAT -- MAX(CASE WHEN THEN)
- 牛客网华为机试题java,华为机试题
- daemontools安装和使用
- python的发展前景与展望_Python语言在未来的发展前景
- 携程工作感言(一)(6.30)
- su sudo 命令
- 360Replugin插件启动Activity慢导致ANR的问题
- matlab再现像,基于matlab的数字显微全息像的再现-光学专业论文.docx
- 识骨寻踪第十二季/全集Bones迅雷下载
- TRIZ创新方法——TRIZ创新思维与方法
- Android拍照识别车牌号SDK
热门文章
- codesys 简单案例_CODESYS-2.3-EXAMPLE 基于CODESYS 2.3的工程应用案例程序 - 下载 - 搜珍网...
- jupyter notebook代码运行一直没有反应, 只显示*号
- 3月14日 | ICLR 2023预讲会深度学习领域顶会专场一
- Distinct + rownum 用法
- Vue知识点整理(一)基础知识
- 电竞盛宴ROG Day:不止是一场信仰集结的狂欢
- html5绘制直线和曲线,SVG基础 | 绘制SVG直线、折线和多边形
- 处理ueditor添加表格没有边框的问题
- 聊一聊我对测试开发的看法
- 使用go-cqhttp结合python作自己的机器人(下)