AIDL进程间通讯

本文介绍了一个使用Android Interface Definition Language (AIDL)实现的客户端和服务端远程过程调用的例子。通过创建一个简单的加法服务,展示了如何定义AIDL接口、在客户端绑定服务并调用方法。
摘要由CSDN通过智能技术生成

客户端:定义一个和Java同级的aidl文件


// IMyAidlInterface.aidl
package com.example.administrator.performancetest;

// Declare any non-default types here with import statements

interface IMyAidlInterface {

    int add(int a,int b);
}

绑定服务端的service,并调用其方法:

package com.example.administrator.newprojectformvp.proj.impl.view;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.newprojectformvp.R;
import com.example.administrator.newprojectformvp.proj.base.view.BaseFragment;
import com.example.administrator.newprojectformvp.utils.ToastUtil;
import com.example.administrator.performancetest.IMyAidlInterface;

/**
 * Created by Administrator on 2017/4/16.
 */
public class SecondPageFragment extends BaseFragment {

    private TextView mTitleTextView;
    private String title;
    private Button btn_remote;


    IMyAidlInterface iMyAidlInterface;

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            iMyAidlInterface = null;
        }
    };

    @Override
    public int getContentView() {
        return R.layout.fragment_secondpage;
    }

    @Override
    protected void inintContentView(View viewContent) {

        mTitleTextView = (TextView) viewContent.findViewById(R.id.tv_title);
        btn_remote = (Button) viewContent.findViewById(R.id.btn_remote);

        //绑定远程服务
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.example.administrator.performancetest", "com.example.administrator.performancetest.IRemoteService"));
        getActivity().bindService(intent, conn, Context.BIND_AUTO_CREATE);


        btn_remote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                try {
                    ToastUtil.showToast(getActivity(), "远程调用计算 2+2="+iMyAidlInterface.add(2, 2) + "");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }


            }
        });
        if (!TextUtils.isEmpty(title)) {
            mTitleTextView.setText(this.title);
        }
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unbindService(conn);
    }
}

服务端:将客户端新建的aidl文件复制到服务端



自定义一个service:

package com.example.administrator.performancetest.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.example.administrator.performancetest.IMyAidlInterface;

/**
 * Created by Administrator on 2017/4/18.
 * 用于AIDL远程调用的service
 */
public class IRemoteService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return iBinder;
    }

    private IBinder iBinder = new IMyAidlInterface.Stub(){

        @Override
        public int add(int a, int b) throws RemoteException {
            Log.d("IRemoteService","收到远程请求,请求参数:a:"+a+" b:"+b);
            return  a+b;
        }
    };

}

清单文件中进行配置:

<service android:name=".service.IRemoteService"
    android:exported="true"
    android:process=":remote"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值