Skip to content Skip to sidebar Skip to footer

Shared Preferences Not Setting Boolean Inside Boot_received Broadcast Receiver

I am trying to create the option to start my service on boot. The broadcast receiver works great by itself, but when I add in the option it never sets it to true. Here is the code.

Solution 1:

I had to change this...

SharedPreferencesprefs= context.getSharedPreferences("startatboot",0);

to this...

SharedPreferencesprefs= PreferenceManager.getDefaultSharedPreferences(context);



publicvoidonReceive(Context context, Intent intent) {
    Stringaction= intent.getAction();

    if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
      SharedPreferencesprefs= PreferenceManager.getDefaultSharedPreferences(context);
      booleanstartatboot= prefs.getBoolean("startatboot", false);
      if (startatboot) {
          context.startService(newIntent(context, MyService.class));
      }

Post a Comment for "Shared Preferences Not Setting Boolean Inside Boot_received Broadcast Receiver"