原因為Implicit Broadcast Exceptions這篇文章。
擷取開頭描述
As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. However, several broadcasts are currently exempted from these limitations.
....
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
不能在manifest中broadcast receivers註冊broadcasts為implicit 。這段話浪費我好久久。
原來intent 有分為 implicit 和 explicit。
- Explicit Intent: Explicit intent names the component.
- Implicit Intent: Implicit Intents have not specified a component.
光從字面上,根本搞不懂發生什麼。還是從範例來了解:
Implicit activity call
With an intent filter you create action for your activity so other apps can call your activity via an action:
<activity android:name=".BrowserActivitiy" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
</intent-filter>
</activity>
.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(intent);
Explicit activity call
You make a call that indicates exactly which activity class to use:
Intent intent = new Intent(this, ActivityABC.class);
startActivity(intent);
解讀後,從上面例子同理反推,
當Build Version >Android 8.0,在manifest.xml <receiver>時自己定義<intent-filiter> action(Implicit Receiver),更改成Explicit Receiver。
若要合法使用<intent-filiter> action,只能參考Implicit Broadcast Exceptions文章下面列的這些反應動作。
github找到的例子
manifest.xml中修改註冊為Explicit Receiver
在activity中把intent改為explicit intent
補充 下面連結有提到 為什麼要限制Implicit Broadcast的解釋
JobIntentService for background processing on Android O
再補上簡體中文的機制說明和解決辦法
从源码角度看Android_O_AMS新特性
Android O 8.0 BroadcastReceiver 注册工具
JobIntentService for background processing on Android O
再補上簡體中文的機制說明和解決辦法
从源码角度看Android_O_AMS新特性
Android O 8.0 BroadcastReceiver 注册工具
沒有留言:
張貼留言