Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a service when the Android device is turned on?

Tags:

android

How can I start a Service when the Android device is turned on and the OS is running?

like image 997
Adham Avatar asked Oct 05 '11 23:10

Adham


1 Answers

Add to AndroidManifest.xml:

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

<service android:name=".YourService" />

<receiver android:name="com.your.package.AutoStart">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>

Create class AutoStart.java:

public class AutoStart extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Intent startServiceIntent = new Intent(context, YourService.class);
        context.startService(startServiceIntent);       
    }

}
like image 120
ciscogambo Avatar answered Oct 06 '22 12:10

ciscogambo