客户端:定义一个和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"/>