Android开发:解决高版本8.0系统接收不到广播的问题

做有关广播的项目时,发现高版本的android系统,对于广播的接收变得越来越严格,

导致自己写的demo中收不到广播,看了官网上的一些介绍    附上链接 :

https://developer.android.google.cn/about/versions/oreo/features/background-broadcasts 

将应用安装到Android7系统上面可正常使用,但是安装到Android8上面广播的接收不到广播

 

原因: 

Android8在静态广播的使用上做了一些限制具体可查看:https://developer.android.google.cn/about/versions/oreo/android-8.0.html

 Android开发:解决高版本8.0系统接收不到广播的问题_第1张图片

解决办法:

使用动态广播代替静态广播

receiver = new MyReceiver();
IntentFilter filter = new IntentFilter("android.intent.action.MyReceiver");
getActivity().registerReceiver(receiver, filter);
Intent intent = new Intent("android.intent.action.MyReceiver");
getActivity().sendBroadcast(intent);

 

Android开发:解决高版本8.0系统接收不到广播的问题_第2张图片

 

你可能感兴趣的:(技巧)