1. 程式人生 > >Android studio 通過AIDL來實現加法運算

Android studio 通過AIDL來實現加法運算

1:首先滑鼠右鍵點選專案新建AIDL Folder

這裡寫圖片描述

2:在 aidl檔案右鍵新建AIDL File

這裡寫圖片描述

3:刪除 或者無視裡面的內容,並寫下如下程式碼,並Rebuild project。

package com.dt.aidltest;


interface IMyAidlInterface {

    int add(int num1,int num2);

}

4:新建一個Service ,主要是用來實現AIdl,並把它暴露給客戶端,以方便客戶端呼叫

public class AIDLService extends Service {

    @Nullable
@Override public IBinder onBind(Intent intent) { return new IMyAidlInterface.Stub() { @Override public int add(int num1, int num2) throws RemoteException { return num1 + num2; } }; } }

5:在 AndroidManifest 中 宣告該Service 並新增 action ,這裡好向必須新增 action,否則在客戶端會報空指標異常。

        <service android:name=".AIDLService">
            <intent-filter>
                <action android:name="android.intent.action.AIDLServoce" />
            </intent-filter>
        </service>

6:點選File,新建一個module (客戶端)

這裡寫圖片描述

7:在客戶端新建AIDL 資料夾,並把服務端的aidl檔案copy到客戶端,包括包名,要保持一致。最後在 rebuild project 。

這裡寫圖片描述

8:接下來就簡單了,首先繫結service,通過service,拿到AIDL介面,IMyAidlInterface。

        Intent intent = new Intent();
        intent.setAction("android.intent.action.AIDLServoce");
        bindService(intent, conn, BIND_AUTO_CREATE);
     protected IMyAidlInterface iMyAidlInterface;
     protected ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

9:拿到 iMyAidlInterface 後,就可以呼叫它的方法了

 try {
    int result = iMyAidlInterface.add(num1,num2);
    edt3.setText(result+"");
} catch (RemoteException e) {
    e.printStackTrace();
}

最後,客戶端程式碼如下:

MainActivity 的程式碼 如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    protected EditText edt1;
    protected EditText edt2;
    protected Button btnAdd;
    protected EditText edt3;
    protected IMyAidlInterface iMyAidlInterface;
    protected ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
    private int num1;
    private int num2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        initView();
        initService();
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_add) {
            getNums();
            try {
               int result = iMyAidlInterface.add(num1,num2);
                edt3.setText(result+"");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    private void getNums() {
        num1 = Integer.parseInt(edt1.getText().toString().trim());
        num2 = Integer.parseInt(edt2.getText().toString().trim());

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService();
    }

    private void initService() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.AIDLServoce");
        bindService(intent, conn, BIND_AUTO_CREATE);
    }

    private void unbindService() {
        unbindService(conn);
        conn = null;
    }

    private void initView() {
        edt1 = (EditText) findViewById(R.id.edt1);
        edt2 = (EditText) findViewById(R.id.edt2);
        btnAdd = (Button) findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(MainActivity.this);
        edt3 = (EditText) findViewById(R.id.edt3);
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.dt.aidl_client.MainActivity">

    <EditText
        android:id="@+id/edt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="30dp" />

    <EditText
        android:id="@+id/edt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="呼叫AIDL來計算" />

    <EditText
        android:id="@+id/edt3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false" />

</LinearLayout>