Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Check
- 아이폰7
- simulator
- 신도림
- 스마트폰
- error
- afterdelay
- Xcode
- 포켓볼
- 앱스토어
- swift3
- 공략
- Bitcode
- 해몽
- UIView
- swift
- LG유플러스
- 신도림 테크노마트
- IOS
- 보라카이
- 샘플
- loop
- setting
- 페이백
- 얻는법
- GCD
- Example
- 포켓몬 GO
- push
- UITableView
Archives
- Today
- Total
도래울
Android Service Lifecycle 본문
Service Lifecycle
Next, we want to override some of the main lifecycle methods:
onCreate()
Called when the service is created first time.onStartCommand()
Called when the service is started.onDestroy()
Called when the service is terminated.
To do that, you can use Eclipse tool Source→Override/Implement Methods and select those three methods.
At this point, in spirit of producing a minimally working app at each stage of learning, we’ll write just a little code that logs a note in each of the overridden methods. So the shell of our service looks like this:
package com.marakana.yamba3; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class UpdaterService1 extends Service { static final String TAG = "UpdaterService"; // @Override public IBinder onBind(Intent intent) { // return null; } @Override public void onCreate() { // super.onCreate(); Log.d(TAG, "onCreated"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // super.onStartCommand(intent, flags, startId); Log.d(TAG, "onStarted"); return START_STICKY; } @Override public void onDestroy() { // super.onDestroy(); Log.d(TAG, "onDestroyed"); } }
As in all major classes, I like to add the TAG constant because I use Log.d() quite a bit. | |
onBind() is used in bound services to return the actual implementation of something called a Binder. Since we are not using a bound service, we can just return null here. | |
onCreate() is called when the service is initially created. It is not called for subsequent startService() calls, so it is a good place to do work that needs to be done only once during the life of a service. | |
onStartCommand() is called each time service receives a startService() intent to get started. A service that is already stated could get multiple requests to start again, and each will cause onStartCommand() to execute. | |
onDestroy() is called just before the service is destroyed by stopService() request. This is a good place to clean up things that might have been initialized in onCreate() . |
Update the Manifest File
Now that we have the shell of our service, we have to define it in the manifest file just like any other main building block, otherwise we won’t be able to call our service. Simply open AndroidManifest.xml, click on the right-most tab to see the raw XML code, and add the following within the <application>
element:
...
<application android:icon="@drawable/icon" android:label="@string/app_name">
...
<service android:name=".UpdaterService" /> <!-- -->
...
</application>
...
<application android:icon="@drawable/icon" android:label="@string/app_name">
...
<service android:name=".UpdaterService" /> <!-- -->
...
</application>
...
Services are equal to activities as Android building blocks, so they appear at the same level in the manifest file.
'개발 > Android' 카테고리의 다른 글
Get Phone State When Someone is calling using BroadcastReceiver Example (2) | 2016.02.05 |
---|---|
Android Eclipse로 Debugging (0) | 2016.02.05 |
안드로이드 Activity LifeCycle (0) | 2016.02.05 |
[안드로이드] 현재 시간 알기 (0) | 2016.02.05 |
같은 안드로이드폰, 하지만 뭔가 다르다? 제조사별 UI 차이 (0) | 2016.02.05 |
Comments