最終更新:
moonlight_aska 2013年08月04日(日) 18:44:22履歴
サービスを起動/停止する方法は,
バインドを利用する方法では, Context#bindService/unbindServiceメソッドを利用する.

- インテントを利用する.
- バインドを利用する.
バインドを利用する方法では, Context#bindService/unbindServiceメソッドを利用する.

- TestService.java
- Serviceクラスを継承した実装クラスを作成する.
- Service#onBindメソッドに起動時の処理を実装する.
- Service#onUnbindメソッドに終了時の処理を実装する.
package com.moonlight_aska.android.service02;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class TestService extends Service {
// 開始時にコール
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "サービス起動", Toast.LENGTH_LONG).show();
return null;
}
// 終了時にコール
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "サービス終了", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
}
- IMyService.aidl
- AIDLファイルを作成し, サービスのインタフェースを定義する.
package com.moonlight_aska.android.service02;
interface IMyService {
}
- AndroidManifest.xml
- マニフェストファイルにサービスの宣言を登録する.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moonlight_aska.android.service02"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
ndroid:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.moonlight_aska.android.service02.ServiceActivity"
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=".TestService">
<intent-filter>
<action android:name="com.moonlight_aska.android.service02.IMyService" />
</intent-filter>
</service>
</application>
</manifest>
- ServiceActivity.java
- サービスを指定して, インテントのインスタンスを取得する.
- ServiceConnectionインタフェースを実装したクラスを作成し, そのインスタンスを取得する.
- bindServiceメソッドで, サービスを起動する.
- unbindServiceメソッドで, サービスを停止する.
package com.moonlight_aska.android.service02;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
public class ServiceActivity extends Activity implements View.OnClickListener {
private Button btn = null;
private Intent intent = null;
private boolean mode = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
btn = (Button)findViewById(R.id.service_btn);
btn.setOnClickListener(this);
// サービスクラスを指定
intent = new Intent(IMyService.class.getName());
}
private ServiceConnection connect = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.service_btn) {
if (!mode) {
// サービスの起動
bindService(intent, connect, BIND_AUTO_CREATE);
btn.setText(R.string.stop_label);
mode = true;
}
else {
// サービスの停止
unbindService(connect);
btn.setText(R.string.start_label);
mode = false;
}
}
}
}
このページへのコメント
askaです.
タイトル(バインドでサービスを起動/停止する)に関連する部分については,
一応コメントor本文中で解説を入れているつもりですが, どの辺りのコメントが
必要ですか?
ソースにコメントを書いて欲しい