tags:

views:

198

answers:

3

Hey everyone, I'm trying to write an app that consists of an activity that manages a background service. However, I want to implement a user setting for automatically starting the service up at boot time. I have user settings implemented with SharedPreferences and I have the services starting up at boot by using a BroadcastReceiver and listening for BOOT_COMPLETED.

However, I can't figure out a good way to implement a setting so that the service is only started at boot if said setting is enabled. I can think of a few cheap ways to do this (such as messing with onCreate() in the service, or creating/checking for a file on SD card) but I want to follow good practice.

There must be a good way to do this because there's tons of apps out there that do it, I just can't find anything online about how to do it.

Thanks

+1  A: 

Unless I am missing something, you already have all the pieces...you just need to put them togther.

Since your BroadcastReceiver is almost surely what starts your service after receiving BOOT_COMPLETED, simply check your SharedPreferences from the BroadcastReceiver by using the Context supplied in onReceive and calling getSharedPreferences().

If your start-on-boot setting is present and enabled, start your service from the BroadcastReceiver, if not, do not start your service.

Simple!

jscharf
this won't work though, because the context supplied does not have the sharedPreferences of my app. I just tried this and it does not work...
Flakkar
Really? What is returned by:SharedPreferences prefs = context.getSharedPreferences([YOUR PREFS NAME HERE],0);Is 'prefs' null, or your settings just not in it? FYI I use the method I described above and it works fine.
jscharf
A: 

Obviously you need to set this up as a BOOT_COMPLETED receiver in your manifest, but this code works - it's taken almost straight from one of my apps...

public class Booter extends BroadcastReceiver {

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

    if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
      SharedPreferences prefs = context.getSharedPreferences("PACKAGENAME_preferences",0);
      if (prefs.getBoolean("startatboot",false)) {
        ... DO STUFF HERE ...
      }
    }
  }
}
vmlinuz
thanks this really helped I guess I was giving getSharedPreferences() the wrong name, now it works!
Flakkar
+1  A: 

The best way to do this is use PackageManager.setComponentEnabledSetting(): http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting(android.content.ComponentName, int, int)

So simply decide whether you want your receiving to be enabled or not by default by setting android:enabled in the manifest. Then use this API to explicitly enable or disable the component at run time as desired. If the component is disabled, at boot it will not be available, and thus not receive the broadcast.

Fwiw, using this approach is much better than checking a preference when receiving the intent, because it avoids the system having to launch your app each boot. As more and more apps want to launch during boot to possibly do something, having them not launch at all if they don't need to is a good thing.

hackbod