AIDL(Android Interface Definition Language) android 接口定义语言
AIDL IPC 多个应用程序 多线程
Binder 只有IPC 多个应用程序 没有多线程
Messenger 只有IPC 没有多线程
在eclipse中,定义包下的aidl接口文件,可以自动生成,AS中需要手动编译一次
package com.imlzj.aidl;
//计算两个数的合 (未编译之前文件)
interface IImlzjAidl{
int add(int num1,int num2);
}
public class IRemoteService extends Service{
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span>当客户端绑定的时候就会执行
<span style="white-space:pre"> </span>*/
<span style="white-space:pre"> </span>public IBinder onBind(Intent intent){
<span style="white-space:pre"> </span>return iBinder;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>Private IBinder iBinder=new IImoocAidl.Stub(){
<span style="white-space:pre"> </span><pre name="code" class="java"><span style="white-space:pre"> </span>public int add(int num1,int num2) throws RemoteException{
<span> </span>Log.d("lzj","收到了远程的请求,输入的参数是"+num1+"和"+num2);
<span style="white-space:pre"> </span>return num1+num2;
<span> </span>}
<span style="white-space:pre"> </span>};
<span style="white-space:pre"> </span>
}
接下来是客户端,需要将服务端的.aidl文件复制到客户端(注意包名)
public class MainActivity extends Activity{
<span style="white-space:pre"> </span>private EditText mEtNum1;<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java"><span style="white-space:pre"> </span>private EditText mEtNum2;
<span style="white-space: pre;"></span><pre name="code" class="java"><pre name="code" class="java" style="font-size:14px;"><span style="white-space:pre"> </span>private EditText mEtRes;
</pre><pre name="code" class="java" style="font-size:14px;"><span style="white-space:pre"> </span>private Button mBtnAdd;
<span style="white-space:pre"> </span>IImlzjAidl iImlzjAidl;
<span style="white-space:pre"> </span>private ServiceConnection conn=new ServiceConnection(){
<span style="white-space:pre"> </span>//绑定上服务的时候
<span style="white-space:pre"> </span>public void onServiceConnected(ComponentName name ,IBinder service){
<span style="white-space:pre"> </span>//拿到了远程服务
<span style="white-space:pre"> </span>iImlzjAidl=IImlzjAidl.Stub.asInterface(service);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>//当服务断开的时候
<span style="white-space:pre"> </span>public void onServiceDisconnected(){
<pre name="code" class="java" style="font-size:14px;"><span style="white-space:pre"> </span>iImlzjAidl=null;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState){
<span style="font-size:14px; white-space: pre;"> </span><span style="font-size:14px;">super.onCreate(</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:12px;">savedInstanceState</span></span><span style="font-size:14px;">);</span>
<span style="font-size:14px;"><span style="white-space:pre"> </span>setContentView(R.layout.activity_main);</span>
<span style="font-size:14px;"><span style="white-space:pre"> </span></span>
<span style="white-space:pre"> </span> initView();
<span style="white-space:pre"> </span>//软件一启动就绑定服务
<span style="white-space:pre"></span><pre name="code" class="java" style="font-size:14px;"><span style="white-space:pre"> </span>bindService()
<span style="white-space:pre"> </span>}
</pre><pre name="code" class="java" style="font-size:14px;"><span style="white-space:pre"> </span>private void initView(){
<span style="white-space:pre"> </span><pre name="code" class="java" style="font-size:14px;"><span style="white-space:pre"> </span>mEtNum1=(EditeText) findViewById(R.id.et_num1);<pre name="code" class="java" style="font-size:14px;"><span style="white-space:pre"> </span>mEtNum1=(EditeText) findViewById(R.id.et_num2);
<span style="white-space:pre"> </span>mEtNum1=(EditeText) findViewById(R.id.et_res);
</pre><pre name="code" class="java" style="font-size:14px;"><span style="white-space:pre"> </span>mBtnAdd=(Button) findViewById(R.id.btn_add);
<span style="white-space:pre"> </span>mBtnAdd.setOnClickListener(this);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>Public void onClick(View v){
<span style="white-space:pre"> </span>//调用远程服务
<span style="white-space:pre"> </span>int num1=Integer.parseInt(mEtNum1.getText().toString);<span style="white-space:pre"> </span><pre name="code" class="java" style="font-size:14px;"><span style="white-space:pre"> </span>int num2=Integer.parseInt(mEtNum2.getText().toString);
<span> </span>try{<span> </span>
<span style="white-space:pre"> </span>int res=iImlzjAidl.add(num1,num2);
<span style="white-space:pre"> </span>mEtRes.setText(res+"");
}catch(RemoteException e){
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>mEtRes.setText("计算错误了");
<span style="white-space:pre"> </span>}
<span style="font-size:10px;"><span style="font-family:Courier New;"><span style="white-space:pre"> </span></span></span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>private void bindService(){
<span style="white-space:pre"> </span><pre name="code" class="java"><pre name="code" class="java" style="font-size:14px;"><span> </span>//获取到服务端
<span> </span>Intent intent=new Intent();
<span> </span>//新版本必须显示Intent启动绑定服务
<span style="font-size:14px;"> </span><span style="font-size:14px;">intent.setComponent(new ComponentName("</span><span style="font-size:12px;"><span style="font-family: Arial, Helvetica, sans-serif;">com.imlzj.aidl</span><span style="font-family: Arial, Helvetica, sans-serif;">","com.imlzj.aidl.IRemoteService"));</span></span>
<span style="font-family: Arial, Helvetica, sans-serif;font-size:12px;"> </span><span style="font-family:Arial, Helvetica, sans-serif;font-size:12px;"> </span><span style="font-size:10px;"><span style="font-family:Arial;"> </span><span style="font-family:Courier New;">bindService(intent,conn,Context.BIND_AUTO_CREATE);</span></span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>protected void onDestroy(){
<span style="white-space:pre"> </span>super.onDestroy();
<span style="white-space:pre"> </span>unbindService(conn);
<span style="white-space:pre"> </span>}
}