如何使用AIDL-非同步實作

如何使用AIDL-非同步實作

如果想要透過另外一個Process幫你執行程式, 你可以利用AIDL去跟Process溝通,
如何使用AIDL中有簡單介紹一下AIDL,
但是其實有很多細節並沒有說明, 所以這邊來實作非同步的範例。
更多的AIDL可以參考[官網]。

這個範例比較特別, 必須開兩個app才能達到IPC的效果。
首先第一個範例是遠端APP, 只需要建立AIDL檔案跟一支Service就好了。

  • Remote Side

首先先建立AIDL檔案, 由於這個是非同步的方式,
因此我們要使用oneway關鍵字, 並且回傳值是void,
你會發現我們傳入的參數是一個callback物件,
所以下面還會在宣告一個aidl的callback interface。

package example.givemepass.aidlremotedemo;
import example.givemepass.aidlremotedemo.IRemoteAIDLCallback;
interface IRemoteAIDL {
   oneway void getRemoteName(IRemoteAIDLCallback callback);
}

這支aidl是用來傳給client端的callback,
到時候Client端只要覆寫這個方法就可以收到msg了。

// IRemoteAIDLCallback.aidl
package example.givemepass.aidlremotedemo;

interface IRemoteAIDLCallback {
    void handleMsg(String name);
}

再來就是Service部分, 這邊的Service是用來給Client呼叫,
因此使用我們的aidl檔案所產生的interface來實作成一個Binder物件,
透過service的onBind傳出去, 當結束的時候, 再指派成null即可。

那在我們的Service內有宣告一個flag,
是用來判斷client是否中斷聯繫, 如果有中斷聯繫,
則結束迴圈。

public class RemoteService extends Service {
    private boolean flag;
    private final IRemoteAIDL.Stub remoteBinder = new IRemoteAIDL.Stub(){
        @Override
        public void getRemoteName(IRemoteAIDLCallback callback) throws RemoteException {
            while(!flag) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                callback.handleMsg("remote service");
            }
        }
    };
    @Override
    public IBinder onBind(Intent intent) {
        return remoteBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        flag = true;
        return super.onUnbind(intent);
    }
}

Remote Side記得要開filter給系統辨識是要連結到這個Service。
menifest

<service
    android:name=".RemoteService"
    android:process=":remote">
    <intent-filter>
        <action android:name="service.remote" />
    </intent-filter>
</service>

  • Client Side

在這邊我們宣告了一個聯繫的Button跟一個中斷聯繫的Button,
TextView是用來顯示Remote端傳回來的訊息。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="example.givemepass.aidlclientdemo.MainActivity">
    <Button
        android:text="connect remote"
        android:id="@+id/connect_remote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:layout_toRightOf="@id/connect_remote"
        android:text="disconnect remote"
        android:id="@+id/disconnect_remote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_below="@id/connect_remote"
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

這邊是我們的主程式

public class MainActivity extends AppCompatActivity {
    private IRemoteAIDL mService;
    private TextView result;
    private Button connectRemote;
    private Button disconnectRemote;
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = IRemoteAIDL.Stub.asInterface(service);
            try {
                mService.getRemoteName(new IRemoteAIDLCallback.Stub(){
                    StringBuffer sb = new StringBuffer();
                    @Override
                    public void handleMsg(final String name){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                sb.append(name + "\n");
                                result.setText(sb);
                            }
                        });
                    }
                });
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = (TextView) findViewById(R.id.result);
        connectRemote = (Button) findViewById(R.id.connect_remote);
        disconnectRemote = (Button) findViewById(R.id.disconnect_remote);
        connectRemote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("service.remote");
                intent.setPackage("example.givemepass.aidlremotedemo");
                bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
            }
        });
        disconnectRemote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(mConnection);
            }
        });
    }
}

如何使用bindService中,
有示範怎麼使用Service,
一開始我們就宣告了ServiceConnection物件,
比較不一樣的地方是我們拿到IBinder物件,
要透過我們所簽訂的AIDL轉成所對應的Binder物件,

mService = IRemoteAIDL.Stub.asInterface(service);

接著來實作我們的AIDL Callback方法。

mService.getRemoteName(new IRemoteAIDLCallback.Stub(){
    StringBuffer sb = new StringBuffer();
    @Override
    public void handleMsg(final String name){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                sb.append(name + "\n");
                result.setText(sb);
            }
        });
    }
});

這樣一來就可以拿到在Remote Service所傳來的字串,
把它擷取起來裝到TextView內。

而當我們把聯繫的Button按下去以後,
就可以進行Service的連結。

Intent intent = new Intent();
intent.setAction("service.remote");
intent.setPackage("example.givemepass.aidlremotedemo");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

這邊要明確的跟系統說我們要連到哪個package name以及它的filter字串是甚麼,
否則會找不到對應的Service。

一開始一定要將兩個apk都安裝到手機內, 否則會找不到Remote。
來看結果

Remote端程式碼
Client端程式碼