简体   繁体   中英

BroadcastReceiver is not working after reboot

My BroadcastReceiver works fine, but when I reboot my phone, all notifications stop working.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a52780.nontifications"
    android:installLocation="internalOnly">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".AlarmReceiver"
            android:priority="999">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

            // Get id & message from Intent

            int notificationId = intent.getIntExtra("notificationId", 0);
            String message = intent.getStringExtra("text");

            // when notif. is tapped, call MainActivity

            Intent mainIntent = new Intent(context, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);

            NotificationManager myNotificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            // PREPARE NOTIF

            Notification.Builder builder = new Notification.Builder(context);
            builder.setSmallIcon(android.R.drawable.ic_dialog_info)
                    .setContentTitle("Wake up!")
                    .setContentText(message)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(contentIntent);

            myNotificationManager.notify(notificationId, builder.build());
        }
}

Also I tried to put android:enabled="true" and android:exported="false" in receiver. But it didn't work. Application installs in phone memory not on the SD card. BroadcastReceiver should works, anyway it doesn't work after reboot

I just found the solution!

AndroidManifest.xml

<receiver
        android:name="com.example.a52780.nontifications.AlarmReceiver"
        android:enabled="true"
        android:exported="false"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <service
        android:name="com.example.a52780.nontifications.BootService"
        android:enabled="true"
        android:exported="false"/>

Also I added BootService.java

private void setAlarm(Intent intent) {
    int notificationId = intent.getIntExtra("notificationId", 0);
    String text = intent.getStringExtra("text");
    Intent mainIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);
    NotificationManager myNotificationManager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle("Wake up!")
            .setContentText(text)
            .setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent);
    myNotificationManager.notify(notificationId, builder.build());
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    setAlarm(intent);
    Intent service = new Intent(this, BootService.class);
    stopService(service);
}

AlarmReceiver.java

private static final String BOOT_COMPLETED =
        "android.intent.action.BOOT_COMPLETED";
private static final String QUICKBOOT_POWERON =
        "android.intent.action.QUICKBOOT_POWERON";

@Override
public void onReceive(Context context, Intent intent) {

    // Get id & message from Intent
    int notificationId = intent.getIntExtra("notificationId", 0);
    String message = intent.getStringExtra("text");

    String action = intent.getAction();
    if (BOOT_COMPLETED.equals(action) ||
            QUICKBOOT_POWERON.equals(action)) {

        Intent service = new Intent(context, BootService.class);

        intent.putExtra("notificationId", notificationId);
        intent.putExtra("text", message);

        context.startService(service);
    }
    }

I changed code in MainActivity that calls intent as well. Old:

Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
            intent.putExtra("notificationId", notificationId);
            intent.putExtra("text", text);
            intent.putExtra("alarmStartTime", alarmStartTime);               
            PendingIntent alarmIntent = PendingIntent.getBroadcast(MainActivity.this, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);

Idk why, but it works after adding

intent.setAction("android.intent.action.BOOT_COMPLETED");
intent.setAction("android.intent.action.QUICKBOOT_POWERON");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM