android run background service on startup

Solutions on MaxInterview for android run background service on startup by the best coders in the world

showing results for - "android run background service on startup"
Eleonora
16 Apr 2018
1//AndroidManifest.xml:
2 <receiver android:name=".BootBroadcastReceiver" >   
3            <intent-filter>   
4                <action android:name="android.intent.action.BOOT_COMPLETED" />   
5            </intent-filter>   
6        </receiver> 
7//
8
9
10//Add permission in your AndroidManifest.xml as:
11<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
12</uses-permission>
13
14//In code part BootBroadcastReceiver:
15public class BootBroadcastReceiver extends BroadcastReceiver {     
16    static final String ACTION = "android.intent.action.BOOT_COMPLETED";   
17    @Override   
18    public void onReceive(Context context, Intent intent) {   
19        // BOOT_COMPLETED” start Service    
20        if (intent.getAction().equals(ACTION)) {   
21            //Service    
22            Intent serviceIntent = new Intent(context, StartOnBootService.class);       
23            context.startService(serviceIntent);   
24        }   
25    }    
26}   
27/*
28if 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.
29*/