blog

AIDLのデモ

LocalAppでも同じaidlファイルを使用 2 RemoteAppでサービスを公開し、onBind()でそのサービスを返します。...

Dec 4, 2020 · 2 min. read
シェア

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ファイルは、リモートとローカル間の通信プロトコルを定義する単なるインターフェースファイルです。

Read next

H5 モバイル入力に関する考察

1.onBlurイベントは、clearイベントをトリガーするために右側に絶対位置の要素を追加することでclearイベントを処理します。また、クリアボタンがフォーカスを失ったときに非表示にする必要があります。そして、空ボタンのzIndexがどれだけ高くても、blurイベントしかトリガーできず、空ボタンのクリックイベントは必ずblurイベントをトリガーすることがわかりました。

Dec 4, 2020 · 1 min read