android AIDL的简单使用

借助AIDL能够实现进程间的通信,记录一下如何使用;


1.创建一个AIDL文件,这里命名ICalAIDL:

package com.test.android.test;

// Declare any non-default types here with import statements
interface ICalAIDL {
    int add(int x, int y);
    int sub(int x, int y);
}

如果是AndroidStudio build一下项目,就会在app/build/generated/source/aidl/debug下自动生成一个ICalAIDL.java文件。


2.服务端代码

public class CalService extends Service {

    private static final String TAG = "CalService";

    private ICalAIDL.Stub mBinder = new ICalAIDL.Stub() {
        @Override
        public int add(int x, int y) throws RemoteException {
            return x + y;
        }

        @Override
        public int sub(int x, int y) throws RemoteException {
            return x - y;
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind");
        return mBinder;
    }
}

创建一个ICalAIDL.Stub对象,该类在自动生成的ICalAIDL.java文件中。重写定义的接口的两个方法,这两个方法就是我们创建的AIDL文件内的2个方法。Stub这个类是Binder的子类,在onBind()方法中返回该对象。最后注册这个Service,当前让它运行在独立的进程中。


3.客户端代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    private Button mBindService;
    private Button mUnBindService;
    private Button mAdd;
    private Button mSub;

    private ICalAIDL mICalAIDL;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected");
            mICalAIDL= ICalAIDL.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected");
            mICalAIDL = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBindService = (Button) findViewById(R.id.bindService);
        mUnBindService = (Button) findViewById(R.id.unbindService);
        mAdd = (Button) findViewById(R.id.add);
        mSub = (Button) findViewById(R.id.sub);
        mBindService.setOnClickListener(this);
        mUnBindService.setOnClickListener(this);
        mAdd.setOnClickListener(this);
        mSub.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bindService: {
                Intent intent = new Intent(MainActivity.this, CalService.class);
                bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
                break;
            }
            case R.id.unbindService: {
                unbindService(mConnection);
                break;
            }
            case R.id.add: {
                if(mICalAIDL != null) {
                    try {
                        int addAns = mICalAIDL.add(1, 2);
                        Toast.makeText(MainActivity.this, "1 + 2 = " + addAns, Toast.LENGTH_SHORT).show();
                    } catch (RemoteException e) {
                        Log.i(TAG, e.toString());
                    }
                } else {
                    Toast.makeText(MainActivity.this, "连接服务器异常,请重新连接", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            case R.id.sub: {
                if(mICalAIDL != null) {
                    try {
                        int subAns = mICalAIDL.sub(30, 12);
                        Toast.makeText(MainActivity.this, "30 - 12 = " + subAns, Toast.LENGTH_SHORT).show();
                    } catch (RemoteException e) {
                        Log.i(TAG, e.toString());
                    }
                } else {
                    Toast.makeText(MainActivity.this, "连接服务器异常,请重新连接", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            default:
                break;
        }
    }
}

创建ServiceConnection对象,重写2个方法,在onServiceConnected()方法内通过IBinder获得ICalAIDL对象。之后就可以调用ICalAIDL对象的方法了。


这里记录一下如何实现进程间通信,原理并不是很懂~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值