안드로이드 시스템이 부팅될 때 "android.intent.action.BOOT_COMPLETED" 액션을 포함한 Intent 가 Broadcasting 되므로 이 인텐트를 수신하여 처리하는 BroadcastReceiver 를 구현하면 부팅시에 자동으로 실행될 로직을 작성할 수 있다.

다음은 시스템이 부팅될 때 BroadcastReceiver가 실행되고 BroadcastReceiver 에서 Service 프로그램을 실행하고 Service는 Notification을 제목 표시줄에 출력하는 예이다.

나머지 구성 파일은 여기를 참조하세요

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.android.hello" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CALL_PHONE"/> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".ServiceStarterActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyNotifyService" /> <receiver android:name=".BootBroadcastReceiver" 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> </application> </manifest>


BootBroadcastReceiver

package test.android.hello; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BootBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){ Intent myIntent = new Intent(context, MyNotifyService.class); context.startService(myIntent); } } }


Posted by cwisky