Dino
Dino

Reputation: 561

Broadcast Receiver not restarting after reboot

I am trying to get my Broadcast Receiver to run right after boot. From what I am aware the broadcast receiver will run regardless of rebooting and I just learnt that, but my problem is that I have set it up to run at midnight every night and I don't want to wait till midnight to run it once as that defeats the purpose. But I need it to run right after reboot once. But it is not running. Can someone see if I am doing something wrong? I am trying this on a Galaxy S4 and S6 and get not log message stating that it has rebooted.

This is my Manifest file, as you can see it is the necessary permissions.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<receiver
        android:name=".StartActivityAtBootReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
 </receiver>

And in my StartActivityAtBootReceiver I have the onReceive with the call to the main activity which will start the broadcast receiver.

public void onReceive(Context context, Intent intent) {
    Log.e("LOGS", "Start Activity after Rebooted ");
    Intent rebootIntent = new Intent(context, MainActivity.class);
    rebootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(rebootIntent);
}

In my MainActivity I have the following which calls the broadcast receiver in another class that extends the Broadcast Receiver.

//run from boot or from button on screen
protected void runFromOutside() throws ParseException {
    checkIfStartingNow();
    startTheClock();
    finish(); //close the app/view
}

//check if starting now pops up message to state that it is staring now
protected void checkIfStartingNow() throws ParseException {
//does some checks and displays a message popup
}

  protected void startTheClock() {

    // Set the alarm to run at midnight every night
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 00);
    //calendar.set(Calendar.MINUTE, 01);
    // Get the AlarmManager Service
    mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    // Create an Intent to broadcast to the Shhh
    mNotificationReceiverIntent = new Intent(MainActivity.this, Shhh.class);

    // Create an PendingIntent that holds the NotificationReceiverIntent
  //  mNotificationReceiverPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, mNotificationReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mNotificationReceiverPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, mNotificationReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    //Set repeating alarm that checks every minute.
    mAlarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mNotificationReceiverPendingIntent);

    // set true for alarm
    settings.edit().putBoolean(NotificationOn, true).apply();
    Log.e("LOGS", "Entered Start the midnight alarm");
}

Upvotes: 2

Views: 1418

Answers (1)

Dino
Dino

Reputation: 561

OK So I figured it out, the reason it was not working was because the <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> was not in the correct place in the Android Manifest file, so in the end I had to move it outside of the application tag (the correct place to put it), then the reboot worked. So make sure that you have the correct XML layout in your Android Manifest file as I learnt the hard way as it can cause havoc in your app.

The <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> was inside the application tag so it never worked. VERY IMPORTANT. Hope this helps someone else in the future. So make sure your XML tags are in the right place.

Upvotes: 1

Related Questions