※前提条件:本情報はAndroid Studio 1.5.1を基づいて説明してる Serviceの起動方式 †Serviceは以下の二つの起動方式がある、ライフサイクルへの影響がそれぞれ異なる
Context.startService()方式 †起動する際に startService –> onCreate() –> onStart() 停止しても、サービスはバックグラウンドでずっと動作している サービス実装側(ServiceDemo.java) package com.android.service.activity; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class ServiceDemo extends Service { private static final String TAG="Test"; @Override //Service起動された時 public void onCreate() { Log.i(TAG, "Service onCreate--->"); super.onCreate(); } @Override //startService()でServiceを起動する時 public void onStart(Intent intent, int startId) { Log.i(TAG, "Service onStart--->"); super.onStart(intent, startId); } @Override public void onDestroy() { Log.i(TAG, "Service onDestroy--->"); super.onDestroy(); } @Override //startService()でServiceを起動する時,メソッド内「return null」のみ実装すればよい public IBinder onBind(Intent intent) { return null; } } AndroidManifest.xmlの定義 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.service.activity" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" 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=".ServiceDemo" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </service> </application> </manifest> サービス起動側(MainActivity.java) package com.android.service.activity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button startBtn; private Button stopBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startBtn = (Button) findViewById(R.id.startBtn); stopBtn = (Button) findViewById(R.id.stopBtn); startBtn.setOnClickListener(listener); stopBtn.setOnClickListener(listener); } private OnClickListener listener=new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this, ServiceDemo.class); switch (v.getId()) { case R.id.startBtn: startService(intent); break; case R.id.stopBtn: stopService(intent); break; default: break; } } }; } Context.bindService()方式 †バインドする際に bindService -> onCreate() –> onBind() 呼び出し側が停止しない限り、サービスはバックグラウンドでずっと動作している、呼び出し側が停止すれば、サービスも停止する サービス実装側(BindService.java) package com.android.bindservice.activity; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class BindService extends Service{ private static final String TAG="Test"; public class MyBinder extends Binder { //サービスの取得 BindService getService() { return BindService.this; } } public IBinder onBind(Intent intent) { Log.i(TAG, "Service onBind--->"); return null; } // Service作成された時 public void onCreate() { Log.i(TAG, "Service onCreate--->"); } // startService()でServiceを起動する時 public void onStart(Intent intent, int startId) { Log.i(TAG, "Service onStart--->"); } // Service停止する時 public void onDestroy() { Log.i(TAG, "Service onDestroy--->"); } // アンバインドする時 public boolean onUnbind(Intent intent) { Log.i(TAG, "Service onUnbind--->"); return super.onUnbind(intent); } } AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.bindservice.activity" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" 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=".BindService" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </service> </application> </manifest> サービス起動側(MainActivity.java) package com.android.bindservice.activity; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private BindService myservice; private Button startBtn,stopBtn,bindBtn,unbindBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startBtn = (Button)findViewById(R.id.startBtn1); stopBtn = (Button)findViewById(R.id.stopBtn2); bindBtn = (Button)findViewById(R.id.bindBtn3); unbindBtn = (Button)findViewById(R.id.unbindBtn4); startBtn.setOnClickListener(startListener); stopBtn.setOnClickListener(stopListener); bindBtn.setOnClickListener(bindListener); unbindBtn.setOnClickListener(unBindListener); } // Serviceを起動 private OnClickListener startListener = new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, BindService.class); // Serviceを起動 startService(intent); } }; // Serviceを停止 private OnClickListener stopListener = new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, BindService.class); // Serviceを停止 stopService(intent); } }; // コネクションオブジェクト private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { myservice = ((MyService.MyBinder)service).getService(); Log.i("Service", "Connected"); } @Override public void onServiceDisconnected(ComponentName name) { myservice = null; Log.i("Service", "Disconnected"); } }; // Serviceをバインド private OnClickListener bindListener = new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, BindService.class); // Serviceをバインド bindService(intent, conn, Service.BIND_AUTO_CREATE); } }; // Serviceをアンバインド private OnClickListener unBindListener = new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, BindService.class); // Serviceをアンバインド unbindService(conn); } }; } 本当にほしかったのはこういうブログだったんだ コメント: |