サービスには2種類の起動方法がある。
■サービスをスタート
ServiceActivity.java
package info.justoneplanet.android.sample.service; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.widget.Toast; public class ServiceActivity extends Activity { private Intent mIntent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // ブロードキャストレシーバの設定(サービスには直接関係しない) IntentFilter filter = new IntentFilter(SampleService.ACTION); registerReceiver(new ServiceReceiver(), filter); } @Override public void onResume() { super.onResume(); // サービスの起動 mIntent = new Intent(this, SampleService.class); startService(mIntent); } @Override public void onPause() { super.onPause(); // サービスの停止 stopService(mIntent); } /** * テスト用のブロードキャストレシーバ<br> * 実行されるとToastを表示する * @author justoneplanet */ private class ServiceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(getApplicationContext(), "hello", 200).show(); } } }
SampleService.java
package info.justoneplanet.android.sample.service; import java.util.Timer; import java.util.TimerTask; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; public class SampleService extends Service { public static final String ACTION = "SampleService.ACTION"; private Timer mTimer; /** * サービスが一番初めに作られたときに実行される */ @Override public void onCreate() { super.onCreate(); mTimer = new Timer(); Toast.makeText(getApplicationContext(), "onCreate", 1000).show(); } /** * サービスがスタートするごとに実行される<br> * 但しこのメソッドは2.0以降は非推奨でonStartCommandを使うことが推奨されている */ @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); hello(); Toast.makeText(getApplicationContext(), "onStart", 1000).show(); } /** * サービスが無効になり取り除かれるときに実行される */ @Override public void onDestroy() { super.onDestroy(); mTimer.cancel(); Toast.makeText(getApplicationContext(), "onDestroy", 1000).show(); } /** * クライアントで登録したBroadcastReceiverに対してIntentを送る */ public void hello() { mTimer.schedule( new TimerTask() { @Override public void run() { sendBroadcast(new Intent(ACTION)); } }, 200, 2000 ); } @Override public IBinder onBind(Intent intent) { Toast.makeText(getApplicationContext(), "onBind", 1000).show(); return null; } }
stopServiceを呼ばずにアプリを終了した場合は、バックグラウンドで動き続ける。
■サービスをバインド
ServiceActivity.java
package info.justoneplanet.android.sample.service; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.widget.Toast; public class ServiceActivity extends Activity { private Intent mIntent; private SampleService mSampleService; /** * サービスをモニタリングするインターフェース<br> * 状態に応じて各メソッドがinvokeされる */ private ServiceConnection conn = new ServiceConnection() { /** * サービスとの切断時に呼ばれる */ @Override public void onServiceDisconnected(ComponentName name) { Toast.makeText(getApplicationContext(), "onServiceDisconnected", 1000).show(); } /** * サービスとの接続時に呼ばれる */ @Override public void onServiceConnected(ComponentName name, IBinder service) { Toast.makeText(getApplicationContext(), "onServiceConnected", 1000).show(); mSampleService = ((SampleService.LocalBinder)service).getService(); mSampleService.hello(); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // ブロードキャストレシーバの設定(サービスには直接関係しない) IntentFilter filter = new IntentFilter(SampleService.ACTION); registerReceiver(new ServiceReceiver(), filter); } @Override public void onResume() { super.onResume(); // サービスの起動 mIntent = new Intent(this, SampleService.class); bindService(mIntent, conn, Context.BIND_AUTO_CREATE); } @Override public void onPause() { super.onPause(); // サービスの停止 unbindService(conn); } /** * テスト用のブロードキャストレシーバ<br> * 実行されるとToastを表示する * @author justoneplanet */ private class ServiceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(getApplicationContext(), "hello", 200).show(); } } }
SampleService.java
package info.justoneplanet.android.sample.service; import java.util.Timer; import java.util.TimerTask; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.widget.Toast; public class SampleService extends Service { public static final String ACTION = "SampleService.ACTION"; private Timer mTimer = new Timer(); /** * ActivityでサービスのインターフェースにアクセスするためのBinder * @author justoneplanet */ public class LocalBinder extends Binder { public SampleService getService() { return SampleService.this; } } private IBinder mIBinder = new LocalBinder(); /** * バインドされた時に実行される */ @Override public IBinder onBind(Intent intent) { Toast.makeText(getApplicationContext(), "onBind", 1000).show(); return mIBinder; } /** * バインドが解除されtrueを返した後、再度バインドすると実行される */ @Override public void onRebind(Intent intent) { Toast.makeText(getApplicationContext(), "onRebind", 1000).show(); } /** * バインドが解除されたときに実行される */ @Override public boolean onUnbind(Intent intent) { Toast.makeText(getApplicationContext(), "onUnbind", 1000).show(); mTimer.cancel(); return true; } /** * クライアントで登録したBroadcastReceiverに対してIntentを送る */ public void hello() { mTimer.schedule( new TimerTask() { @Override public void run() { sendBroadcast(new Intent(ACTION)); } }, 200, 2000 ); } }