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...

SharedPreferences prefs = context.getSharedPreferences("startatboot",0);

to this...

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);



public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

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

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