Search
 
SCRIPT & CODE EXAMPLE
 

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.
*/
Comment

PREVIOUS NEXT
Code Example
Java :: file to multipartfile in java 
Java :: how to find length of set in java 
Java :: how to create a list in java 
Java :: java string lowercase 
Java :: Youtube video thumbnail not load in android 11 or above progmatically 
Java :: get last element of array java 
Java :: character classes in regex 
Java :: bukkit java connect player to another server in bungeecord 
Java :: java swing jtable different color each row 
Java :: actionlistener java 
Java :: spring data rest partial update 
Java :: java klasse 
Java :: java test file exist 
Java :: how to make an arraylist java 
Java :: java stop executing my program 
Java :: android clear specific sharedpreference value 
Java :: decimal up to 6 places in java 
Java :: american heavy tanks of ww2 
Java :: how to extract decimal vqalue from float in android studio 
Java :: Convert c# to xml string 
Java :: java float get 2 decimals 
Java :: sum of all numbers in array java 
Java :: how to add to a date in android 
Java :: java random seed 
Java :: round off java 2 decimal places 
Java :: change status bar color android programmatically 
Java :: how to check if a string is empty or null in Android Studio 
Java :: spigot respawn player location 
Java :: string startswith java 
Java :: java int array 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =