Android开发之AIDL的使用
AIDL是Android Interface definition language的缩写;它是一种接口定义语言,用来约束两个进程间通讯(IPC)的规则,供编译器生成代码,实现Android设备上两个进程间通讯(IPC),进程之间通讯的信息,首先会被转换成AIDL协议消息,然后发送给对方,对方收到AIDL协议消息后,再转换成相应的对象,由于进程之间的通信信息需要双向转换,所以Android采用代理在背后实现信息的双向转换,代理类由Android编译器自动生成。
进程间通讯就需要创建两个不同的程序,一个作为服务器端,一个作为客户端
一、首先看下服务器端的目录结构
首先创建一个以.aidl为后缀的文件
package com.example.aidl;/** * AIDL 供编译器生成通讯代码 * * @author Administrator * */interface StudentQuery { String QueryStudent(int id);}
两个应用程序之间就是通过QueryStudent进行数据传递,保存以上代码后编译器会自动在gen下生成以.java为后缀的相同名称文件。
在服务器处理应用请求的数据
package com.example.service;import com.example.aidl.StudentQuery;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class StudentQueryService extends Service { private String[] names = { "张三", "赵六", "李四", "陈九", "王五" }; private IBinder binder = new StudentQuerBinder(); @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub //在客户端得到的Binder具有了远程通讯的能力(代理对象) return binder; } private String query(int number) { if (number > 0 && number < 5) { return names[number]; } return null; } /** * 只要继承StudentQuery.Stub就具有了远程通讯的能力 * @author Administrator * */ private final class StudentQuerBinder extends StudentQuery.Stub { @Override public String QueryStudent(int id) throws RemoteException { // TODO Auto-generated method stub return query(id); } }}
二、客户端目录结构
两个程序需要通讯,一定要有协议去制约,所以在客户端也需要定义相同的aidl文件
package com.example.aidl;/** * AIDL 供编译器生成通讯代码 * * @author Administrator * */interface StudentQuery { String QueryStudent(int id);}
客户端的代码
package com.example.aidlclient;import com.example.aidl.StudentQuery;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener { private TextView tv_result = null; private EditText et_number = null; private StudentQuery studentQuery; private StudentConnection conn = new StudentConnection(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.btn_result); btn.setOnClickListener(this); tv_result = (TextView) findViewById(R.id.tv_result); et_number = (EditText) findViewById(R.id.et_number); //激活远程服务 Intent intent = new Intent("com.example.student.query"); //绑定远程服务 bindService(intent, conn, BIND_AUTO_CREATE); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_result: String number = et_number.getText().toString().trim(); if(TextUtils.isEmpty(number)){ Toast.makeText(MainActivity.this, "学号不能为空", Toast.LENGTH_LONG); } else{ int num = Integer.valueOf(number); try { String name = studentQuery.QueryStudent(num); tv_result.setText("查询到的姓名是:" + name); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } break; default: break; } } @Override protected void onDestroy() { // TODO Auto-generated method stub unbindService(conn); super.onDestroy(); } private final class StudentConnection implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub //把代理对象转换成接口对象 studentQuery = StudentQuery.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub studentQuery = null; } }}
这样两个进程间就可以实现通讯了。
Demo下载
点击打开链接
鎶ュ悕瀛︿範鍔犲井淇�/QQ 1602007锛屽叧娉ㄣ€婁笢鏂硅仈鐩熺綉銆嬪井淇″叕浼楀彿
>更多相关文章
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 03-08消息称微软开发内部AI推理模型,或将成为Op
- 03-08美国法院驳回马斯克请求,未阻止OpenAI转型
- 03-08饿了么成立即时配送算法专家委员会 持续全局
- 03-08长安汽车:预计今年底长安飞行汽车将完成试
- 03-08谷歌推出虚拟试穿、AR美妆新功能
相关文章
24小时热门资讯
24小时回复排行
热门推荐
最新资讯
操作系统
黑客防御