Android:重新启动后启动广播无法正常工作



我有一个使用广播服务在重新启动后继续我的待定意图的应用程序,但它无法正常工作。重新启动手机后,我会收到弹出通知,说MyAppsName has stopped working。我为其制作的日志没有出现。

subtest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.idkbro.pashchallenge">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<application
    android:allowBackup="true"
    android:name=".BaseApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <receiver android:name=".AlertReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </receiver>
    <receiver
        android:name=".BootReceiver"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    <service
        android:name=".NotificationService"
        android:exported="true"></service>
</application>

bootReceiver:

@Override
public void onReceive(Context context, Intent intent) {
    Log.i("Xu", "Boot receiver is working");
}

只需在接收器类中添加它:

if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
   Log.i("Xu", "Boot receiver is working");
}

并从清单回收器标签中删除许可和类别意图过滤器,例如:

<receiver
        android:name=".BootReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

最新更新