Android8.0 遇到的坑

1.广播问题

取消了大部分静态注册的广播,如果非要使用静态的广播,发送广播的时候必须指明接收广播的包名和类名。当然也必须在AndroidManifest.xml里面注册

Intent it = new Intent("com.example.test.broadcast");
it.setComponent(new ComponentName("com.example.test",
                        "com.example.test.MyBroadcast"));
sendBroadcast(it);

动态广播的使用没什么改变。

2.启动服务

在之前的版本无论什么时候都可以使用下面的方法启动service

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.test","com.example.test.TestService"));
        startService(intent);

当应用处于后台时:

1.在后台运行的服务在几分钟内会被stop掉,在这段时间内,应用仍可以创建和使用服务。

2.在应用处于后台几分钟后,应用将不能再通过startService创建后台服务,如果创建则抛出以下异常

 Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.test/.TestService }: app is in background uid UidRecord{1e01f93 u0a79 LAST bg:+1m48s665ms idle change:idle procs:1 seq(0,0,0)}

在调试时使用adb shell am startservice -n 来启动services时,出现了下面的错误。

Error: Requires permission not exported from uid 10155

需要系统的签名,于是我把这个app使用了系统签名,接着使用adb 命令,出现下面的错误。

app is in background uid UidRecord{ec023e7 u0a72 CEM  idle change:cached procs:1 seq(0,0,0)}

绝望了,超时了无论如何都不能启动service了。

扫描二维码关注公众号,回复: 5276222 查看本文章

使用startForegroundService()来代替之前大方法,但是要在Sevice中调用startForeground()方法

   @Override
    public void onCreate() {
        super.onCreate();

        startForeground(1,new Notification());
    }

这个方法是带notification的,也就是说,如果想用常驻service,就得让用户知道。

猜你喜欢

转载自blog.csdn.net/qq_32072451/article/details/82016901