Android AIDL使用详解 实现进程间的通信
2015-11-10 19:22
911 查看
在Android中,如果我们需要在不同进程之间实现通信(例如activity与Service之间通信),就需要用到AIDL技术去完成。
1
什么是aidl:aidl是 Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,用于约束两个进程间的通信规则,供编译器生成代码,通过它我们可以定义进程间的通信接口,实现Android设备上的两个进程间的通信icp:interprocess communication :内部进程通信2、
既然aidl可以定义并实现进程通信,那么我们怎么使用它呢?文档/android-sdk/docs/guide/developing/tools/aidl.html中对步骤作了详细描述:1)、创建一个IService服务Android工程,创建一个ImusicService.aidl文件,定义接口,以及需要的抽象方法,并保存在对应的包中,此时会在gen包下面生成对应同名文件package com.example.service; interface ImusicService{ void playMusic(); }
<pre name="code" class="java">package com.example.service; import com.example.service.ImusicService.Stub; import android.app.Service; import 4000 android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.widget.Toast; public class AIDLService extends Service { public Handler handler = new Handler(); public class ServiceBinder extends ImusicService.Stub{ @Override public void playMusic() throws RemoteException { handler.post(new Runnable() { @Override public void run() { Toast.makeText(AIDLService.this, "调用成功 播放音乐", Toast.LENGTH_SHORT).show(); } }); } } public ServiceBinder mServiceBinder = new ServiceBinder(); @Override public IBinder onBind(Intent intent) { Log.v("tag", "onBind(Intent intent)"); return mServiceBinder; } }
3)、在IService的AndroidManifest.xml文件中注册service组件,设置action隐式启动,便于activity启动时去查找调用
<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><service android:name="com.example.service.AIDLService" ><intent-filter ><action android:name="android.intent.action.MUSIC_PLAYER"/></intent-filter></service></application>服务类就创建好了4)、A、创建客户端IClient工程,需要把IService工程下的aidl文件连同它的包一起拷贝到IClient的src下,然后只保留AIDL文件就可以了,此时IClient的gen包下面会对应生成此包,此时这样做的目的便于找到这个服务的接口
package com.example.cient;import com.example.service.ImusicService;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {Button mButton;private ImusicService mservice;private ServiceConnection conn = new ServiceConnection(){@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.i("ServiceConnection", "onServiceConnected() called");mservice = ImusicService.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {Log.i("ServiceConnection", "onServiceDisconnected() called");mservice = null;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mButton = (Button) findViewById(R.id.button1);Intent intent = new Intent();intent.setAction("android.intent.action.MUSIC_PLAYER");bindService(intent, conn, BIND_AUTO_CREATE); //自动创建mButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {mservice.playMusic();} catch (RemoteException e) {e.printStackTrace();}}});}@Overrideprotected void onDestroy() {super.onDestroy();if(conn != null){unbindService(conn);}}}5)最后调试了,先启动IService,再启动IClient
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories