전산쟁이의 카피질

뒤로 검색

부팅시점에 안드로이드 서비스 시작

2010/01/29 17:56

출처 : http://androidgps.blogspot.com/2008/09/starting-android-service-at-boot-time.html

A service that has to be started manually is an oxymoron, so starting a service at boot time is for many applications a must.

My tracklogging service is such an example, which should run whenever the phone is turned on, so that one can refer to the route travelled later. A further example is the much touted CarbonFootprint, which again relies on accurate measurements all the time, and not just when the user has turned it on.

The last post detailed how the TrackloggingService worked, but started the service only when the main activity was launched. Now comes the time to hook it into the Android boot sequence. Here is how:

After boot completes the Android system broadcasts an intent with the action android.intent.action.BOOT_COMPLETED. And now all we need is an IntentReceiver, now called a BroadcastReceiver, to listen and act on it. This is how this class looks:


The key is of course the onReceive() method. I have decided to check that it is actually the Intent I am expecting, but otherwise it is straightforward starting the service and return.

The receiver needs to be declared in the manifest, e.g. with the following entry:


Furthermore this class listen to this specific event needs to be declared in the security settings:


That's it. Now the service will be started as soon a Android has finished booting.
Now we will need something to make this user-configurable...

Tags

부팅, 서비스, 안드로이드
이 페이지는 Textcube 1.10.0 : beta 1 로 구동됩니다 데스크탑 화면