Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

android run background service on startup

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


//Add permission in your AndroidManifest.xml as:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>

//In 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);   
        }   
    }    
}   
/*
if you are talking about device screen on/off then you need to register <action android:name="android.intent.action.USER_PRESENT" /> and <action android:name="android.intent.action.SCREEN_ON" /> for starting your service when user is present or screen is on.
*/
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #android #run #background #service #startup
ADD COMMENT
Topic
Name
6+4 =