폰을 키면 자동으로 서비스가 시작되도록 하기 위해서는 ”android.intent.action.BOOT_COMPLETED” 액션을 받을 브로드캐스트 리시버를 등록해서 처리하여 주면 된다.
출처 : http://androidgps.blogspot.com/2008/09/starting-android-service-at-boot-time.html
1. 브로드케스트 리시버
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class LocationLoggerServiceManager extends BroadcastReceiver { public static final String TAG = "LocationLoggerServiceManager"; @Override public void onReceive(Context context, Intent intent) { // just make sure we are getting the right intent (better safe than sorry) if( "android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { ComponentName comp = new ComponentName(context.getPackageName(), LocationLoggerService.class.getName()); ComponentName service = context.startService(new Intent().setComponent(comp)); if (null == service){ // something really wrong here Log.e(TAG, "Could not start service " + comp.toString()); } } else { Log.e(TAG, "Received unexpected intent " + intent.toString()); } } } |
2. 메니페스트 인텐트필터
1 |
3. user permission
1 |