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问题的更多相关文章

  1. Android studio 使用startService报错:IllegalStateException

    Android 8.0启动service报错:java.lang.RuntimeException和java.lang.IllegalStateException 错误信息: java.lang.Ru ...

  2. Android平台抓取native crash log

    Android开发中,在Java层可以方便的捕获crashlog,但对于 Native 层的 crashlog 通常无法直接获取,只能通过系统的logcat来分析crash日志. 做过 Linux 和 ...

  3. 阿里安卓面试分析: Android应用的闪退(crash)问题跟踪和解析

    一:问题描述    闪退(Crash)是客户端程序在运行时遭遇无法处理的异常或错误时而退出应用程序的表现,请从crash发生的原因分类与解决方法.在出现crash后如何捕捉并分析异常这两个问题给出自己 ...

  4. Android NDK定位.so文件crash代码位置

    参考:http://blog.csdn.net/xyang81/article/details/42319789 问题:      QRD8926_110202平台的Browser必现报错.(去年的项 ...

  5. Android怎样捕获应用的crash信息

    转载请注明出处:http://blog.csdn.net/fishle123/article/details/50823358 我们的应用不可避免的会发生crash,假设是在调试阶段,我们能够使用Lo ...

  6. [Android Pro] Service (startservice , bindservice , unbindservice, stopService)

    1: startService -------stopService (this will call onDestroy) 2: bindService -------unbindService    ...

  7. Android NDK开发Crash错误定位[转]

    使用 ndk-stack 的时候需要你的 lib 编译为 debug版的,通常需要下面的修改: 1. 修改 android.mk,增加,为 LOCAL_CFLAGS 增加 -g 选项 2. 修改 ap ...

  8. Android基调(十六)- Service:startService()、stopService()、bindService()、unbindService()加

    直行 第一 另外一种 第三种 总结 开门见山 开启服务有三种情况:假设直接使用服务,则没有必要进行绑定,可是假设要使用服务里面的方法.则要进行绑定. 具体的启动情况有下: ①调用startServic ...

  9. Android NDK开发Crash错误定位

    在Android开发中,程序Crash分三种情况:未捕获的异常.ANR(Application Not Responding)和闪退(NDK引发错误).其中未捕获的异常根据logcat打印的堆栈信息很 ...

随机推荐

  1. iOS开发- Xcode插件- 规范凝视生成器VVDocumenter 自己的见解

    xcode升级  VVDocumenter 插件失效怎么办?? 首先给个完整的安装參考:http://www.th7.cn/Program/IOS/201405/212030.shtml  參考这个能 ...

  2. 学习鸟哥的Linux私房菜笔记(12)——系统监视2

    四.控制进程 kill :语法 kill [-signal] PID     向进程传送一个特定的讯号,默认为15(终结) kill -l  :列出所有可以由kill传递的讯号 1 :重启进程 2 : ...

  3. 【BZOJ 1014】 [JSOI2008]火星人prefix

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1014 [题意] 让你在线查询最长公共前缀. 支持单节点修改; 插入操作; [题解] / ...

  4. Swift语言实现代理传值

    需求:利用代理实现反响传值(下面样例採用点击第二个视图控制器中的button来改变第一个视图控制器中的Label的内容) 一.创建RootViewController import Foundatio ...

  5. 数据集(benchmark)、常用数据集的解析(cifar-10、)

    What is the class of this image ? 主要是以下常见的数据集,用以衡量算法的分类准确率: mnist.cifar-10.cifar-100stl-10 svhn.ILSV ...

  6. Android 对.properties文件的读取

    /** * * @param filepath .properties文件的位置 */ public void checkFileExists(String filepath){ File file ...

  7. hadoop编程技巧(8)---Unit Testing (单元测试)

    所需的环境: Hadoop相关jar包裹(下载版本的官方网站上可以): 下载junit包裹(新以及). 下载mockito包裹: 下载mrunit包裹: 下载powermock-mockito包裹: ...

  8. ASP.NET中进度条的简单应用

    <html xmlns="http://www.w3.org/1999/xhtml" id="mainWindow"> <head> & ...

  9. iOS 注册或登录页面(UILable,UITextField,UIButton)

    注册或登录页面 例如下面的附图 1,为了在这里展示UITextField文本框关联的键盘设置.在这里,"password"和"判定password"关联键盘被设 ...

  10. 还在使用vc6.0吗??vs2010吧

    每个人在面对新的事物的时候,总是充满了排斥感,当vista和win7来临的时候,我们还在留恋xp,说xp是如何的好,win7是如何的不给力,当然,我们必须承认,xp是一款优秀的操作系统,这个我们无法否 ...