Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Service on boot but not entire Android app

I am working with Android.

I have an app I am working on uses an Activity to setup specific user input values that are then used by a service to provide alerts based on those values. Doing the research I determined how I could get the app to start up when the phone boots, however, what I really want is to have the service start but not have the app load to the screen. Currently the entire app loads to the screen when I turn on the device and then I have to exit out of it.

I have downloaded similar programs that have interfaces for settings but otherwise run in the background. How is that done?

like image 421
user2196720 Avatar asked Mar 21 '13 19:03

user2196720


1 Answers

First you have to create a receiver:

public class BootCompletedReceiver extends BroadcastReceiver {

    final static String TAG = "BootCompletedReceiver";

    @Override
    public void onReceive(Context context, Intent arg1) {
        Log.w(TAG, "starting service...");
        context.startService(new Intent(context, YourService.class));
    }
}

Then add permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

and register intent receiver:

<receiver android:name=".BootCompletedReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

After this is done, your application (Application class) will run along with services, but no Activities.

Ah, and don't put your application on SD card (APP2SD or something like that), because it has to reside in the main memory to be available right after the boot is completed.

like image 180
lenik Avatar answered Oct 05 '22 05:10

lenik