Android Service在工作中也用的很多,但是AIDL就用的很少了,感觉也很生疏,之前在公司也有同事做过技术讲座,而且也看过一些技术文章,但是感觉依然朦朦胧胧的。现在从事教学工作后,把AIDL又看了一遍,发现其实并不用理解的那么复杂,其实很简单的一个RPC(IPC)机制。
使用AIDL涉及到的前提是:需要跟其他应用的Service进行数据交换或者是方法调用。(也就是远程操作其他Service)。否则如果没有数据交换或方法调用,直接使用startService()即可;本地Service更加无需AIDL。
明确这个大前提后,来看AIDL所涉及的几个知识点:
1.ServiceConnection接口:实现这个接口后可以实现Service连接状态的回调方法onServiceConnected(ComponentName component, IBinder service)和onServiceDisconnected(ComponentName component)。
2.IBinder接口
3.Binder类:Binder可以想象成一个内存共享对象,这个对象只有方法暴露出来给客户端调用。
4.Stub静态内部类:其实就是继承了Binder和实现了自己定义的AIDL接口的一个类(所谓的代理),就是一个实现了自己定义的接口的一个Binder。
5.实现了Parcelable序列化接口的自定义Java Bean:如果传输简单数据,根本没必要。(顺便说两句:1.要想把数据存储到磁盘或者是通过网络传输,一定要序列化;2.最为经常使用的Bundle其实就是一个Parcelable,可以通过Bundle来理解Parcelable)
使用AIDL要注意的地方:
1.文件名字一定要以.aidl结尾
2..aidl文件的包名很重要,Service端和调用端包名一定要保持一致。
先上一张图,说明整个调用过程和远程过程调用的原理:
下面贴出工程代码结构图:
下面上代码:
1.IPerson.aidl文件:
package com.wenix.service.aidl; /** *除了基本类型,String,List,Map,CharSequence以外,其他类型一定要导包,即时是同一包下。 */ import com.wenix.service.aidl.Person; interface IPerson{ Person getPerson(in int id); }
package com.wenix.service.aidl; /** *这里声明Person类型的时候是小写的parcelable */ parcelable Person;
package com.wenix.androidaidl; import com.wenix.service.aidl.IPerson; import com.wenix.service.aidl.Person; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class PersonService extends Service { //由于是远程Service,所以客户端肯定不能使用显示Intent来启动Service,所以必须提供ACTION。 public static final String AIDL_ACTION = "com.wenix.intent.action.AIDL_ACTION"; private PersonBinder personBinder; /** * 这里继承了自动生成的Stub静态内部类,并提供了接口方法的实现。 * @author wenix */ public class PersonBinder extends IPerson.Stub{ @Override public Person getPerson(int id) throws RemoteException { //这里只返回了一个简单的自定义对象,其实原理相同,任何复杂对象只要序列化了,都可以返回 return new Person(id, "wenix", "wenix"); } } @Override public IBinder onBind(Intent intent) { //此处返回提供给客户端的Binder对象(内存共享对象) return personBinder; } @Override public void onCreate() { super.onCreate(); //进行数据初始化 personBinder = new PersonBinder(); } }
package com.wenix.androidclient; 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.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import com.wenix.service.aidl.IPerson; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private class PersonServiceConnection implements ServiceConnection{ @Override public void onServiceConnected(ComponentName cmp, IBinder service) { //客户端在这个回调方法中获得Binder对象 IPerson p = IPerson.Stub.asInterface(service); try { //这里调用AIDL规定的接口来获取数据 Log.i(TAG, p.getPerson(1).toString()); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //界面采用动态布局 LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); Button btn = new Button(this); btn.setLayoutParams(params); btn.setText("获取Service数据"); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //启动相应的Service,由于是远程Service,所以不能使用显示Intent Intent service = new Intent("com.wenix.intent.action.AIDL_SERVICE"); PersonServiceConnection conn = new PersonServiceConnection(); bindService(service, conn, Context.BIND_AUTO_CREATE); } }); TextView tv = new TextView(this); tv.setLayoutParams(params); ll.addView(btn); ll.addView(tv); setContentView(ll); } }
工程彻底完成,然后我们运行后点击Button,就可以看到获取的数据:
只需掌握整个流程,AIDL就可以很容易的使用。后期会加入一些使用AIDL的具体实例。