1 RemoteAppとLocalAppが同じaidlファイルを使用
package com.song.test.RemoteTimeService.aidl
interface IRemoteTimeService{
/**
* 現在の時刻を取得する
*/
String requestCurrentTime();
}
public class RemoteTimeService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new RemoteBinder();
}
class RemoteBinder extends IRemoteTimeService.Stub {
public String requestCurrentTime(){
return "remote time " +System.currentTimeMillis();
}
}
}
<service android:name="com.song.test.RemoteTimeService" android:process=":remote" android:exported="true" />
startService(new Intent(this, RemoteTimeService.class));
4 LocalAppは、IRemoteTimeService.aidlファイルのコピーを貼り付けると、同期プロジェクトは、aidlの実装を生成し、リモートプロセスのメソッドを呼び出すことができますリモートサービスをバインドします。
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.song.test","com.song.test.RemoteTimeService"));
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
try {
IRemoteTimeService adService = IRemoteTimeService.Stub.asInterface(iBinder);
String s = adService.requestCurrentTime();
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
bindService(intent, conn, Service.BIND_AUTO_CREATE);
注意事項
1 bindService() でsetComponent() のインテントがリモート・サービスのパッケージ名と完全なクラス名を設定しなければならない場合。
2 RemoteAppのサービスには、必ず android:exported="true "を指定してください。
3 IRemoteTimeServiceは、メソッドのオブジェクトを渡すには、オブジェクトのparcebleを実装する必要があります。
4 aidlファイルは、リモートとローカル間の通信プロトコルを定義する単なるインターフェースファイルです。