Starting a background service when you turn on Android

I need ALWAYS to run a background service that will synchronize my Android application and server. I know how to run it through my application, but when Android shuts down, then the background service will die.

How can I make the background service always work? (Even when the device turns off and then turns on ...)

I need to add my background service to Android startup programs. Any clues?

+5
source share
2 answers

use <action android:name="android.intent.action.BOOT_COMPLETED" />to start your service when you turn on the device.

In AndroidManifest.xml:

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

Add permission to AndroidManifest.xmllike:

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

In the code part BootBroadcastReceiver:

public class BootBroadcastReceiver extends BroadcastReceiver {     
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";   
    @Override   
    public void onReceive(Context context, Intent intent) {   
        // BOOT_COMPLETED" start Service    
        if (intent.getAction().equals(ACTION)) {   
            //Service    
            Intent serviceIntent = new Intent(context, StartOnBootService.class);       
            context.startService(serviceIntent);   
        }   
    }    
}   

EDIT:, ./., <action android:name="android.intent.action.USER_PRESENT" /> <action android:name="android.intent.action.SCREEN_ON" /> , .

+15
(Even when the device turns off and then turns on..

OS ACTION_BOOT_COMPLETED. , :

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

http://blog.gregfiumara.com/archives/82

http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/

+3

All Articles