サービスのライフサイクル
Serviceの[起動]から[終了]までの状態遷移は一直線です。[起動]から[終了]までにonCreate、onStart、onDestroyメソッドが呼び出されます。
[開始]→onCreate→onStart→[実行中]→onDestroy→[終了]
プログラム
ボタンを押すとServiceが開始し、再度ボタンを押すとServiceが停止するプログラムを作りました。また、Activityが終了してもServiceがずっと起動した状態を避けるため、ActivityのonDestroyが呼び出された時にServiceを終了するようにしました。
ServiceTest
CallingServiceTestから呼び出されれるServiceです。
CallingServiceTest
ServiceTestを呼び出すActivityです。Serviceの起動にはstartService、終了にはstopServiceを利用します。
/SGL/src/sgl/test/service/ServiceTest.java
package sgl.test.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class ServiceTest extends Service{
private final String TAG = "ServiceTest";
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(TAG, "Service.onStart");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
Log.i(TAG, "Service.onStartCommand");
return START_STICKY;
}
@Override
public void onDestroy (){
super.onDestroy();
Log.i(TAG, "Service.onDestroy");
}
@Override
public IBinder onBind(Intent arg0) {
//no action
return null;
}
}
/SGL/src/sgl/test/service/CallingServiceTest.java
package sgl.test.service;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class CallingServiceTest extends Activity implements OnClickListener{
private final String TAG = "ServiceTest";
boolean flag = true;
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Activity.onCreate");
intent = new Intent(CallingServiceTest.this, ServiceTest.class);
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
Button button = new Button(this);
button.setText("Service");
button.setOnClickListener(this);
linearLayout.addView(button);
}
public void onClick(View arg0) {
Log.i(TAG, "OnClickListener.onClick");
if (flag){
startService(intent);
flag = false;
} else {
stopService(intent);
flag = true;
}
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "Activity.onResume");
}
@Override
public void onPause() {
super.onPause();
Log.i(TAG, "Activity.onPause");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Activity.onDestroy");
if (!flag) {
stopService(intent);
}
}
}
また、AndroidManifest.xmlに作成したServiceを忘れずに登録します。applicationタグの内部に次のタグを記入します。
<service android:name="ServiceTest"></service>
実行結果
Android端末で次のようなアクションを行いました。
(1) CallingServiceTestの起動 [15:15:08.065]
(2) Serviceボタンの押下 [15:15:10.055]
(3) ホームボタンの押下 [15:15:11.935]
(4) CallingServiceTestの再開 [15:15:15.365]
(5) CallingServiceTestの終了 [15:15:19.625]
本当はstopServiceを呼び出さずにActivityを終了してもServiceが終了せずに実行中であることを確認したかったのですが、少しややこしくなりそうなので次の機会にコーディングしてみようと思います。
関連:Activityのライフサイクルの確認

0 件のコメント:
コメントを投稿