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. 使用python抓取CSDN关注人的全部公布的文章

    # -*- coding: utf-8 -*- """ @author: jiangfuqiang """ import re import ...

  2. 微信小程序的轮播图swiper问题

    微信小程序的轮播图swiper,调用后,怎样覆盖系统的 点,达到自己想要的效果 不多说,先上一图望大家多给意见: 这个是效果图: 微信小程序效果图就成这样子: <view class=" ...

  3. 【25.00%】【vijos P1907】飞扬的小鸟

    描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管或者掉在地上的话,便宣告 ...

  4. 【64.52%】【codeforces 697D】Puzzles

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【Cocos2d-x Lua】数据库封装类型的操作

    Lua数据库封装类型的操作 使用演示样例 lua代码: require("DB") -- 保存一个字符串(数据库中存储的数据都是以字符串的形式保存的) DB:getInstance ...

  6. Redis中的管道(PipeLine)与事物(Transactions)

    Redis中的管道(PipeLine)与事物(Transactions) 序言 Redis中的管道(PipeLine)特性:简述一下就是,Redis如何从客户端一次发送多个命令,服务端到客户端如何一次 ...

  7. WPF 使用 Edge 浏览器

    原文:WPF 使用 Edge 浏览器 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访 ...

  8. 使用Visual Studio 2010 一步一步创建Powershell Module 和 Cmdlet

    现在就用C#写一个超级简单的Module和Cmdlet 1. 在VS中创建一个Library的项目 文件->新建->项目->C#->Class Library 在这里给我们的C ...

  9. wpf控件开发基础(4) -属性系统(3)

    原文:wpf控件开发基础(4) -属性系统(3) 知识回顾 接上篇,上篇我们真正接触到了依赖属性的用法,以及依赖属性的属性元数据的用法,并且也实实在在地解决了之前第二篇提到的一系列问题.来回顾一下 属 ...

  10. LeetCode: Generate Parentheses [021]

    [称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...