最简单的Android Aidl 使用
投稿于 被查看 35273 次 评论:155
最简单的Android Aidl 使用
首先,为什么要用aidl
---------------------------------------------------------------------------------------------------------------------------
aidl其实就是进程间的通信
官方文档特别提醒我们何时使用AIDL是必要的:只有你允许客户端从不同的应用程序为了进程间的通信而去访问你的service,以及想在你的service处理多线程。如果不需要进行不同应用程序间的并发通信(IPC),you should create your interface by implementing a Binder;或者你想进行IPC,但不需要处理多线程的,则implement your interface using a Messenger简单说就是,1、你某个service想被别人使用;2、你想使用其它进程的service
然后,怎么用aidl(一下是一个最简单的使用,从服务端获取一个字符串) ------------------------------------------------------------------------- 步骤: 服务端 1、在服务程序端建立xxx.aidl文件,里面放接口(这个接口要与文件名称一样),写法跟写Java程序一样 2、新建一个Service,这个Service里面实现之前那个aidl文件里面的接口 3、manifest.xml文件注册Service client端 1、copy之前的那个aidl文件过来(要独立一个跟服务端文件夹相同名字的文件夹,否则会报错最后面的那个错误) 2、在activity里面初始化ServiceConnection(用来bind Service的),和获得服务端的service 服务端的service = 接口.Stub.asInterface(service);//在ServiceConnection的onServiceConnected里面实现 3、bindService
实现代码: 先看一下目录结构
ExecuteMyAidlService.aidl文件(注意接口名称要与文件名前边相同)
package aid; interface ExecuteMyAidlService { String sayHello(); }MyAidlService.java文件
package aidlservice; import aid.ExecuteMyAidlService; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class MyAidlService extends Service { private static final String TAG = "MyAidlService"; private ExecuteMyAidlService.Stub mBinder = new ExecuteMyAidlService.Stub() { @Override public String sayHello() throws RemoteException { // TODO Auto-generated method stub return "hello "; } }; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return mBinder; } private void Log(String str) { android.util.Log.d(TAG, "------ " + str + "------"); } @Override public void onCreate() { Log("service create"); } @Override public void onStart(Intent intent, int startId) { Log("service start id=" + startId); } }activity里面没有东西就不贴了
客户端的目录结构
ClientActivity的代码:
package com.example.aidlclienttest; import aid.ExecuteMyAidlService; import android.support.v7.app.ActionBarActivity; 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.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class ClientActivity extends ActionBarActivity { private ExecuteMyAidlService mIaidlServerService = null; private TextView mTextView = null; private Button mButton = null; private ServiceConnection mConnection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { mIaidlServerService = null; } public void onServiceConnected(ComponentName name, IBinder service) { mIaidlServerService = ExecuteMyAidlService.Stub.asInterface(service); //aidl通信 try { String mText = "Say hello: " + mIaidlServerService.sayHello(); mTextView.setText(mText); } catch (RemoteException e) { e.printStackTrace(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_client); mTextView = (TextView)findViewById(R.id.helloword); mButton = (Button)findViewById(R.id.getServiceFromAidl); mButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent service = new Intent("aidl.ExecuteMyAidlService"); service.setAction("aidl.ExecuteMyAidlService"); bindService(service, mConnection,BIND_AUTO_CREATE); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.client, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
这里就实现了最简单的aidl通信的过程 看一下效果 先开启服务端
然后再启动client端,点击bindservice之后
进阶aidl<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPgo8c3Ryb25nPi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTwvc3Ryb25nPgoxoaLI57n7ztLP67TTc2VydmljZbvxyKG21M/zxNijv6Oo1+7SqtOm08OjqQq9q7bUz/PKtc/WcGFyY2VsYWJsZSC907/aCiAgICDOqtXiuPa21M/z0LS49mFpZGzOxLz+W8Dvw+bWu9Do0qq2qNLl0rvPwtXiuPa21M/zvs3Q0MHLyOc6XQo8c3Ryb25nPjwvc3Ryb25nPjxwcmUgY2xhc3M9"brush:java;"> parcelable Object
2、如果activity要往service里面传递数据呢? 【如果是对象一样是要parcelable,(因为client和server的aidl文件是一样的)】 要在接口函数那里加上in 基本类型(int,long,char,boolean等),String,CharSequence,List,Map,其他类型必须使用import导入,即使它们可能在同一个包里,比如 上面的Student,尽管它和IMyService在同一个包中,但是还是需要显示的import进来。
另外,接口中的参数除了aidl支持的类型,其他类型必须标识其方向:到底是输入还是输出抑或两者兼之,用in,out或者inout来表示,一般 in标记,因为大多数情况下输入型参数。
过程中碰到的错误
------------------------------------------------------------------------------------------------------------------------------------------------java.lang.SecurityException: Binder invocation to an incorrect interface
在service端和client端都有.aidl文件,一定要放在单独的包中,因为两个app中的MainActivity的包名不相同,会导致两个.aidl文件内容不同。单独一个包,只放.aidl文件,就能保证包名和文件内容都是相同的。
用户评论