T303 AIDLでプロセス間通信 データ送信(暗黙的intent:implicit)
暗黙的はimplicit
問題点は
サービス開始->データ送信
これはOK
で、サービス停止->データ送信
これも、動作してしまう・・
Manifest.xml
service にintent-filterを記述する
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.efolab.t303" 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" android:process=":another1"> <activity android:name="Main"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:process=":another0" android:name="MService"> <intent-filter> <action android:name="com.efolab.t303.IService"/> </intent-filter> </service> </application> </manifest>
IService.aidl
package com.efolab.t303; interface IService{ int setData(int data); }
Main.java
/* * 複数のサービスを起動するテスト * できないこと * 1)Manifest.xmlのserviceの記述へ intent-filterで同じactionを指定して * 複数起動できるかと思ったが、だめ。 */ package com.efolab.t303; 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.os.RemoteException; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class Main extends Activity { Context context; Activity activity; LinearLayout base; TextView tv; Button btn0 , btn1 , btn2 , btn3; IService serviceIf; ServiceConnection conn = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder binder) { serviceIf = IService.Stub.asInterface(binder); } @Override public void onServiceDisconnected(ComponentName name) { serviceIf = null; } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); activity = (Activity)this; context = getApplicationContext(); base = new LinearLayout(context); base.setOrientation(LinearLayout.VERTICAL); setContentView(base); tv = new TextView(context); btn0 = new Button(context); btn1 = new Button(context); btn2 = new Button(context); btn3 = new Button(context); //setText btn0.setText("サービスを開始します"); btn1.setText("サービスを終了します"); btn2.setText("データを送信します"); btn3.setText("終了します"); //setId btn0.setId(0); btn1.setId(1); btn2.setId(2); btn3.setId(3); //base addView base.addView(tv); base.addView(btn0); base.addView(btn1); base.addView(btn2); base.addView(btn3); //setLisetener btn0.setOnClickListener(l); btn1.setOnClickListener(l); btn2.setOnClickListener(l); btn3.setOnClickListener(l); } @Override public void onDestroy(){ super.onDestroy(); Log.v("Main","onDestroy"); context.unbindService(conn); } OnClickListener l = new OnClickListener() { @Override public void onClick(View v) { switch(v.getId()){ case 0: //サービスを起動する Intent intent = new Intent(IService.class.getName()); context.bindService(intent, conn, Context.BIND_AUTO_CREATE); break; case 1: // サービスのアンバインド処理 context.unbindService(conn); break; case 2: //データを送信する try { int k = serviceIf.setData(100); Toast.makeText(context, "答えを受信="+k , Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case 3: //Activity finish activity.finish(); } } }; }
MSerivce.java
package com.efolab.t303; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.widget.Toast; public class MService extends Service { @Override public IBinder onBind(Intent intent) { Toast.makeText(getApplicationContext(), "MService onBind", Toast.LENGTH_SHORT).show(); return mService; } private final IService.Stub mService = new IService.Stub() { @Override public int setData(int data) throws RemoteException { Log.v("MService","setData"); int ret = 0; for(int i=0;i<data;i++){ ret += i; } return ret; } }; @Override public boolean onUnbind(Intent intent) { Log.v("MService","onUnbind"); return super.onUnbind(intent); } //終了時 onDestroy() @Override public void onDestroy(){ Log.v("MService","onDestroy"); Toast.makeText(getApplicationContext(), "Service サービス停止します", Toast.LENGTH_SHORT).show(); } }