도래울

안드로이드 부팅시 Activity 자동 실행 본문

개발/Android

안드로이드 부팅시 Activity 자동 실행

도래울 2016. 2. 5. 11:47

1.when the OS starts, it will send a Standard Broadcast Action named android.intent.action.BOOT_COMPLETED. 

2.construct a class extended from IntentReceiver to catch the action, and override its abstract method onReceiveIntent(Context context, Intent intent), where you can put your start service code in. 

3.in AndroidManifest.xml, you should add tag 

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> 
to get the permission of android.intent.action.BOOT_COMPLETED.
 In additon, you should add tag 

<action android:name="android.intent.action.BOOT_COMPLETED" />
 in the <intent-filter> of your IntentReceiver class. 

code example: 
xml: 

Code:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> 
<receiver android:name=".OlympicsReceiver" android:label="@string/app_name"> 
    <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</receiver>


java: 

Code:
public class OlympicsReceiver extends IntentReceiver 

    /* the intent source*/ 
    static final String ACTION = "android.intent.action.BOOT_COMPLETED"; 
        
    public void onReceiveIntent(Context context, Intent intent) 
    { 
        if (intent.getAction().equals(ACTION)) 
        { 
                  context.startService(new Intent(context, OlympicsService.class), null);
//start my your service here 

             Toast.makeText(context, "OlympicsReminder service has started!", Toast.LENGTH_LONG).show(); 
        } 
    } 
}


Comments