I wrote a class Autostart that extends BroadcastReceiver. This starts at every reboot of android. Within this class there’re some intents and alarmmanager. Everything runs perfectly. Now I added “preference” to enable/disable this class. The problem is: when I disable this class (using IF/ELSE), and later enable it, I must restart mobile.
How resolve this problem?
public void onReceive(Context context, Intent intent) {
Calendar cal1 = Calendar.getInstance();
[...]
Intent intent2 = new Intent(context, AlarmReceiver.class);
[...]
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
[...]
Move your code that you execute on boot into a static method somewhere. Call that code both from the boot-time
BroadcastReceiverand from your “enable this logic” code path.