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>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljp345775

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值