Android 8通过startService引起crash问题
Android 8.0 不再允许后台service直接通过startService方式去启动,否则就会引起IllegalStateException。解决方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, MyService.class));
} else {
context.startService(new Intent(context, MyService.class));
}
然后必须在Myservice中调用startForeground():
@Override
public void onCreate() {
super.onCreate();
startForeground(1,new Notification());
}
注意:在要开启的service中给notification添加 channelId,否则会出现如下错误:
RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid ch......
完整参考代码如下:
NotificationManager notificationManager;
String notificationId = "channelId";
String notificationName = "channelName";
private void startForegroundService()
{
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建NotificationChannel
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
startForeground(1,getNotification());
}
private Notification getNotification() {
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.myicon)
.setContentTitle("投屏服务")
.setContentText("投屏服务正在运行...");
//设置Notification的ChannelID,否则不能正常显示
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(notificationId);
}
Notification notification = builder.build();
return notification;
}
@Override
public void onCreate() {
super.onCreate();
startForegroundService();
FLog.d(TAG, "[onCreate] MediaServer Service Started.");
}
补充:关于Android 8.0中Service和Notification
后台执行限制 Android 8.0 为提高电池续航时间而引入的变更之一是,当您的应用进入已缓存状态时,如果没有活动的组件,系统将解除应用具有的所有>>>唤醒锁。 此外,为提高设备性能,系统会限制未在前台运行的应用的某些行为。具体而言: 现在,在后台运行的应用对后台服务的访问受到限制。 应用无法使用其清单注册大部分隐式广播(即,并非专门针对此应用的广播)。 默认情况下,这些限制仅适用于针对 O 的应用。不过,用户可以从 Settings 屏幕为任意应用启用这些限制,即使应用并不是以 O 为目标平台。 Android 8.0 还对特定函数做出了以下变更: 如果针对 Android 8.0 的应用尝试在不允许其创建后台服务的情况下使用 startService() 函数,则该函数将引发一个 IllegalStateException。 新的 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行,系统也允许其调用 Context.startForegroundService()。不过,应用必须在创建服务后的五秒内调用该服务的 startForeground() 函数。
参考链接:
https://blog.csdn.net/huaheshangxo/article/details/82856388
Android 8通过startService引起crash问题的更多相关文章
- Android studio 使用startService报错:IllegalStateException
Android 8.0启动service报错:java.lang.RuntimeException和java.lang.IllegalStateException 错误信息: java.lang.Ru ...
- Android平台抓取native crash log
Android开发中,在Java层可以方便的捕获crashlog,但对于 Native 层的 crashlog 通常无法直接获取,只能通过系统的logcat来分析crash日志. 做过 Linux 和 ...
- 阿里安卓面试分析: Android应用的闪退(crash)问题跟踪和解析
一:问题描述 闪退(Crash)是客户端程序在运行时遭遇无法处理的异常或错误时而退出应用程序的表现,请从crash发生的原因分类与解决方法.在出现crash后如何捕捉并分析异常这两个问题给出自己 ...
- Android NDK定位.so文件crash代码位置
参考:http://blog.csdn.net/xyang81/article/details/42319789 问题: QRD8926_110202平台的Browser必现报错.(去年的项 ...
- Android怎样捕获应用的crash信息
转载请注明出处:http://blog.csdn.net/fishle123/article/details/50823358 我们的应用不可避免的会发生crash,假设是在调试阶段,我们能够使用Lo ...
- [Android Pro] Service (startservice , bindservice , unbindservice, stopService)
1: startService -------stopService (this will call onDestroy) 2: bindService -------unbindService ...
- Android NDK开发Crash错误定位[转]
使用 ndk-stack 的时候需要你的 lib 编译为 debug版的,通常需要下面的修改: 1. 修改 android.mk,增加,为 LOCAL_CFLAGS 增加 -g 选项 2. 修改 ap ...
- Android基调(十六)- Service:startService()、stopService()、bindService()、unbindService()加
直行 第一 另外一种 第三种 总结 开门见山 开启服务有三种情况:假设直接使用服务,则没有必要进行绑定,可是假设要使用服务里面的方法.则要进行绑定. 具体的启动情况有下: ①调用startServic ...
- Android NDK开发Crash错误定位
在Android开发中,程序Crash分三种情况:未捕获的异常.ANR(Application Not Responding)和闪退(NDK引发错误).其中未捕获的异常根据logcat打印的堆栈信息很 ...
随机推荐
- 常用Java开发工具类
common: LruLinkedHashMap:一个支持Lru算法的LinkedHashMap. 源码地址:点击打开链接 MD5:MD5工具类 源码地址:点击打开链接 Slicer:可以将List切 ...
- 选iphone5可以正常编译运行 , 但是5s和6和6s都会编译报错
选iphone5可以正常编译运行 但是5s和6和6s都会编译报错 iphone6编译报错iphone5s编译报错 解决办法是,Build settings里面把Architectures里面的$( ...
- Google Android Studio Kotlin
Google Android Studio Kotlin 开发环境配置 Google 近日开发者大会宣布Kotlin成为Android开发的第一级语言,即Android官方开发语言,可见Google对 ...
- 微信小程序开发demo-地图定位
要求要完成的功能: 1.要完成的要点是城市定位. 2.就是切换城市. 首页我们先参照微信小程序开放的官方文档找到: 在这里我们可以找到”当前位置经纬度“ getLocation: function ( ...
- 还是Qt 通过stylesheet或者palette设置背景色的问题
关于Qt,设置一个widget的背景色后,希望子对象不受影响. 很久以前在QtForum上问过一个问题:http://www.qtforum.org/post/94103/setting-backgr ...
- 静态资源命名的注意点以及document.write与innerHTML的区别
今天拿出了去年刚开始学前端的那本书来看,发现好多新东西. 使用下划线和混合大小写不利于SEO! document.write与innerHTML的区别 这个问题大概是初学前端的人才会问的吧,业务代码中 ...
- 防止SQL/XSS攻击
function clean($str) { $str=trim($str); $str=strip_tags($str); $str=stripslashes($str ...
- 并行编程OpenMP基础及简单示例
OpenMP基本概念 OpenMP是一种用于共享内存并行系统的多线程程序设计方案,支持的编程语言包括C.C++和Fortran.OpenMP提供了对并行算法的高层抽象描述,特别适合在多核CPU机器上的 ...
- mac在下面Apache 创 .htaccess档
在设定一个固定的链接将提下面的例子说明样题: 若您的 .htaccess 文件可写.我们能够自己主动改动它.但似乎它不可写,因此我们在下方列出了您 .htaccess 文件里应该增加的URL 重写规则 ...
- IME输入法编程心得
原文:IME输入法编程心得 posted @ 2012-11-30 00:42 from [FreedomShe] 自然语言处理的输入法作业成品没有做出来,但不想再在蛋疼的Win32上面耗费时间了,整 ...