1. 程式人生 > >sendBroadcast無法接收訊息可能原因

sendBroadcast無法接收訊息可能原因

Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers. If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that do not target your app specifically).

意思靜態註冊的廣播在8.0以上是不能用,推薦用程式碼註冊廣播。
解決方案可以這麼做:

配置:

<receiver android:name="MyStart">

    <intent-filter>
<action android:name="com.example.y.myapplication" />
</intent-filter>
</receiver>
程式碼:
Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.example.y.myapplication","com.example.y.myapplication.MyStart"));
                getApplicationContext().sendBroadcast(intent);