I have a BroadcastReceiver which will handle some actions once boot has been completed. However, the methods that will be invoked from my onReceive method require passing an Activity:
if (intent!!.action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.i(LOG_TAG, "onReceive (ACTION_BOOT_COMPLETED)");
doSomeActionWithActivity(activity);
}
In order to do this, I've created a constructor for my receiver (which I've called BootReceiver:
public BootReceiver(Activity activity) {
this.activity = activity;
}
But although this would resolve my initial issue, I got an error from the manifest file:
.BootReceiver has no default constructor
To resolve this issue, I decided to register and unregister the receiver programmatically, so that I can pass Activity to it.
In onCreate():
Log.i(LOG_TAG, "Registering the receiver (BootReceiver)");
mReceiver = new BootReceiver(SplashActivity.this);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
registerReceiver(mReceiver, intentFilter);
And, in onDestroy():
if (mReceiver != null) {
Log.i(LOG_TAG, "Unregistering the receiver (BootReceiver)");
unregisterReceiver(mReceiver);
}
After testing my app again, I got the following error:
java.lang.IllegalArgumentException: Component class ...BootReceiver does not exist in ...
pointing to a part of my code where I do this:
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
In the above code, I want to be restarting any alarms set when the device reboots. Also, I'm using this file from Remindly, an open-source app as a guide/reference, for the above code.
What would be the correct way of restarting alarms and being able to pass Activity to my BootReceiver class.