What context does BroadcastReceivers get when listening to BOOT_COMPLETED?

Android AlarmManagers lose all registered registered alarms when the phone loses power.

I use the following broadcast receiver to start when android loads:

public class AlarmBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Cursor alarmCursor = MainActivity.dbHelper.loadAlarms();
            // Iterate through every stored alarm and set those alarms.
            // ....
            alarmCursor.close();
        }
    }
}
  • When the onReceive broadcast receiver starts at system startup, what context parameter is provided to the method? I need to know the context because I need the context to cancel the alarms set in this context.

  • I assume that calling MainActivity.dbHelper.loadAlarms () is unsafe because MainActivity is not initialized when the system boots. Or is it safe because dbhelper and loadAlarms () are all initialized and declared static?

+4
2

onReceive , ? , , .

Context onReceive(). . .

, PendingIntent, Context, . Context, .

MainActivity.dbHelper.loadAlarms() MainActivity . , dbhelper loadAlarms() static?

dbHelper static ( onCreate()), . , , - , , Activity . , Activity static. .

+1

, Context BroadcastReceiver ( ApplicationContext), : 1) DBHelper, Activity. Singleton . 2) AlarmManager Service. , onReceive() Alarms

0

Source: https://habr.com/ru/post/1664807/


All Articles