T304 AIDLでプロセス間通信 明示的intent :explicit)
問題あり
このソースでは
終了処理してからもデータの受け渡しができてしまう
なぜかは不明。
従って、いまのところは暗黙的インテント方式で行う!
暗黙的インテント方式でも「同じ」!!
動作
明示的はexplicit
開始と終了
context.startService(intent)で開始しているのでcontext.stopService()で終了すること
Manifest.xml
明示的intentでサービス起動するため
filterの記述は不要
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.efolab.t304" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="Main"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- サービスへのintent-filterの記述は不要 --> <service android:process=":another" android:name="MService"></service> </application> </manifest>
IService.aidl
起動しかしないのでinterfaceの記述のみ
package com.efolab.t304; interface IService{ }
Main.java
package com.efolab.t304; /* * 明示的(explicit)にAIDLを使ってサービスを起動するテスト * 1)AIDLを作成 * 2)MService.java作成(onBindでスタブを返すサービスの実装) * 3)呼び出し側-- * 3-1)ServiceConnectionの宣言 * 3-2)明示的:explicit インテントの宣言と実装 * 4)Manifestにserviceを追記(processも) */ import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class Main extends Activity { Context context; LinearLayout base; TextView tv; Button btn0 ,btn1; IService serviceIf; //AIDLインターフェースの宣言 Intent serviceIntent; ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { serviceIf = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { serviceIf = IService.Stub.asInterface(service); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = getApplicationContext(); base = new LinearLayout(context); base.setOrientation(LinearLayout.VERTICAL); setContentView(base); //control tv = new TextView(context); btn0 = new Button(context); btn0.setText("サービス起動"); btn1 = new Button(context); btn1.setText("サービス終了"); //addView base.addView(tv); base.addView(btn0); base.addView(btn1); /* * 明示的インテント * 呼び出すサービスクラスを直に記述する */ serviceIntent = new Intent(context , MService.class); btn0.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { context.bindService(serviceIntent, conn, Context.BIND_AUTO_CREATE); } }); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { context.unbindService(conn); } }); } }
MService.java
onBindでスタブを返す
package com.efolab.t304; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; /** * * @author kamogashiratsuyoshi * onBindでスタブを返す */ public class MService extends Service { //interfaceのスタブを実装 final IService.Stub binder = new IService.Stub() { }; @Override public IBinder onBind(Intent intent) { Toast.makeText(getApplicationContext(), "MService 起動", Toast.LENGTH_SHORT).show(); //スタブを返す return binder; } @Override public void onDestroy(){ Toast.makeText(getApplicationContext(), "Mservice 終了", Toast.LENGTH_SHORT).show(); } }