首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Android AIDL(Android Interface Definition Language)引见

2012-07-24 
Android AIDL(Android Interface Definition Language)介绍在网上看到一篇介绍AIDL的文章,自己把代码在2.2

Android AIDL(Android Interface Definition Language)介绍

在网上看到一篇介绍AIDL的文章,自己把代码在2.2的sdk上跑了一下,稍微更改了一下原文中的代码(下文中的代码都是自己编译通过后使用的代码)并且添加了xml文件,大家可以按照这个代码自己实现一遍。

以下部门来自网上其他朋友的文章(由于出处太多,无法找到原始的地址)

================================================================


  1. package?com.android.aidltest;? ?
  2. import?android.app.Service;? import?android.content.Intent;?
  3. import?android.os.IBinder;? import?android.os.RemoteCallbackList;?
  4. import?android.os.RemoteException;? import?android.util.Log;?
  5. ? public?class?MyService?extends?Service?{?
  6. ????public?void?onCreate()?{? ????????printf("service?create");?
  7. ????}? ?????
  8. ????public?void?onStart(Intent?intent,?int?startId)?{? ????????printf("service?start?id?=?"?+?startId);?
  9. ????????callback(startId);? ????}?
  10. ????? ?
  11. ????@Override? ????public?IBinder?onBind(Intent?intent)?{?
  12. ????????//?TODO?Auto-generated?method?stub? ????????printf("service?on?bind");?
  13. ????????return?mBinder;? ????}?
  14. ????? ????public?void?onDestroy()?{?
  15. ????????printf("service?on?destroy");? ????????super.onDestroy();?
  16. ????}? ?????
  17. ????public?boolean?onUnbind(Intent?intent)?{? ????????printf("service?on?unbind");?
  18. ????????return?super.onUnbind(intent);? ????}?
  19. ????? ????public?void?onRebind(Intent?intent)?{?
  20. ????????printf("service?on?rebind");? ????????super.onRebind(intent);?
  21. ????}? ?????
  22. ????private?void?printf(String?str)?{? ????????Log.e("TAG","#######################---"+str+"-------");?
  23. ????????? ????}?
  24. ????? ????void?callback(int?val)?{?
  25. ????????final?int?N?=?mCallbacks.beginBroadcast();? ????????for?(int?i?=?0;?i?<?N;?i++)?{?
  26. ????????????try?{? ????????????????mCallbacks.getBroadcastItem(i).actionPerformed(val);?
  27. ????????????}catch(RemoteException?e)?{? ?????????????????
  28. ????????????}? ????????}?
  29. ????????mCallbacks.finishBroadcast();? ????}?
  30. ????? ????private?final?ITaskBinder.Stub?mBinder?=?new?ITaskBinder.Stub()?{?
  31. ????????????????? ????????@Override?
  32. ????????public?void?unregisterCallback(ITaskCallback?cb)?throws?RemoteException?{? ????????????printf("service?on?unregisterCallback");?
  33. ????????????//?TODO?Auto-generated?method?stub? ????????????if(cb!=null)?
  34. ????????????????mCallbacks.unregister(cb);? ????????}?
  35. ????????? ????????@Override?
  36. ????????public?void?stopRunningTask()?throws?RemoteException?{? ????????????printf("service?on?stopRunningTask");?
  37. ????????????//?TODO?Auto-generated?method?stub??????????? ????????}?
  38. ????????? ????????@Override?
  39. ????????public?void?registerCallback(ITaskCallback?cb)?throws?RemoteException?{? ????????????printf("service?on?registerCallback");?
  40. ????????????//?TODO?Auto-generated?method?stub? ????????????if(cb!=null)?
  41. ????????????????mCallbacks.register(cb);? ????????}?
  42. ????????? ????????@Override?
  43. ????????public?boolean?isTaskRunning()?throws?RemoteException?{? ????????????printf("service?on?isTaskRunning");?
  44. ????????????//?TODO?Auto-generated?method?stub? ????????????return?false;?
  45. ????????}? ????};?
  46. ????? ????final?RemoteCallbackList<ITaskCallback>?mCallbacks?
  47. ????=?new?RemoteCallbackList<ITaskCallback>();? }?

?4. aidltest.java

  1. package?com.android.aidltest;? ?
  2. import?android.app.Activity;? import?android.content.ComponentName;?
  3. import?android.content.Context;? import?android.content.Intent;?
  4. import?android.content.ServiceConnection;? import?android.os.Bundle;?
  5. import?android.os.IBinder;? import?android.os.RemoteException;?
  6. import?android.util.Log;? import?android.view.View;?
  7. import?android.view.View.OnClickListener;? import?android.widget.Button;?
  8. ? public?class?AidlTest?extends?Activity?{?
  9. ????private?Button?btnOk;? ????private?Button?btnCancel;?
  10. ????? ????/**?Called?when?the?activity?is?first?created.?*/?
  11. ????@Override? ????public?void?onCreate(Bundle?savedInstanceState)?{?
  12. ????????super.onCreate(savedInstanceState);? ????????setContentView(R.layout.main);?
  13. ????????? ????????btnOk?=?(Button)findViewById(R.id.btn_ok);?
  14. ????????btnCancel?=?(Button)findViewById(R.id.btn_cancel);? ?????????
  15. ????????btnOk.setText("Start?Service");? ????????btnCancel.setText("Stop?Service");?
  16. ????????? ????????btnOk.setOnClickListener(new?OnClickListener()?{?
  17. ????????????public?void?onClick(View?v)?{? ????????????????onOkClick();?
  18. ????????????}? ????????});?
  19. ????????? ????????btnCancel.setOnClickListener(new?OnClickListener()?{?
  20. ????????????public?void?onClick(View?v)?{? ????????????????onCancelClick();?
  21. ????????????}? ????????});?
  22. ????}? ????void?onOkClick()?{???
  23. ????????printf("clicked?ok");? ????????Bundle?args?=?new?Bundle();?
  24. ? ????????Intent?intent?=?new?Intent(this,?MyService.class);?
  25. ????????intent.putExtras(args);? ?
  26. ????????bindService(intent,?mConnection,?Context.BIND_AUTO_CREATE);? ????????startService(intent);????????????????????
  27. ????}? ?
  28. ????void?onCancelClick()?{? ????????printf("clicked?cancel");?
  29. ????????Intent?intent?=?new?Intent(this,?MyService.class);? ????????unbindService(mConnection);?
  30. ????}? ?????
  31. ????private?void?printf(String?str)?{? ????????Log.e("TAG",?"##################-------"+str+"-----");?
  32. ????}? ????ITaskBinder?mService;?
  33. ????? ????private?ServiceConnection?mConnection?=?new?ServiceConnection()?{?
  34. ????????public?void?onServiceConnected(ComponentName?className,? ????????????????IBinder?service)?{?
  35. ????????????mService?=?ITaskBinder.Stub.asInterface(service);? ?????????????
  36. ????????????try?{? ????????????????mService.registerCallback(mCallback);?
  37. ????????????}catch?(RemoteException?e)?{? ?????????????
  38. ????????????}? ????????}?
  39. ????????public?void?onServiceDisconnected(ComponentName?className)?{? ????????????mService?=?null;?
  40. ????????}? ????};?
  41. ????? ????private?ITaskCallback?mCallback?=?new?ITaskCallback.Stub()?{?
  42. ????????@Override? ????????public?void?actionPerformed(int?actionId)?throws?RemoteException?{?
  43. ????????????//?TODO?Auto-generated?method?stub? ????????????printf("callback?id?=?"?+?actionId);?
  44. ????????}? ????};?
  45. }?

?5. xml文件

AndroidMenifest.xml

  1. <?xml?version="1.0"?encoding="utf-8"?>? <manifest?xmlns:android="http://schemas.android.com/apk/res/android"?
  2. ??????package="com.android.aidltest"? ??????android:versionCode="1"?
  3. ??????android:versionName="1.0">? ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">?
  4. ????????<activity?android:name=".AidlTest"? ??????????????????android:label="@string/app_name">?
  5. ????????????<intent-filter>? ????????????????<action?android:name="android.intent.action.MAIN"?/>?
  6. ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>? ????????????</intent-filter>?
  7. ????????</activity>? ?????????
  8. ????????<service?android:name=".MyService"?>? ????????????<intent-filter>?
  9. ????????????????<action?android:name="com.android.aidltest.START_MYSERVICE"?/>? ????????????????<category?android:name="android.intent.category.DEFAULT"?/>?
  10. ????????????</intent-filter>? ????????</service>?
  11. ????????? ????</application>?
  12. ????<uses-sdk?android:minSdkVersion="8"?/>? ?
  13. </manifest>??

?main.xml

  1. <?xml?version="1.0"?encoding="utf-8"?>? <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?
  2. ????android:orientation="vertical"? ????android:layout_width="fill_parent"?
  3. ????android:layout_height="fill_parent"? ????>?
  4. <TextView??? ????android:layout_width="fill_parent"??
  5. ????android:layout_height="wrap_content"?? ????android:text="@string/hello"?
  6. ????/>? <Button??
  7. ????android:id="@+id/btn_ok"? ????android:layout_width="wrap_content"?
  8. ????android:layout_height="wrap_content"? ????android:text="@string/btn_ok"??
  9. ????/>? <Button??
  10. ????android:id="@+id/btn_cancel"? ????android:layout_width="wrap_content"?
  11. ????android:layout_height="wrap_content"? ????android:text="@string/btn_cancel"??
  12. ????/>? </LinearLayout>?

string.xml

  1. <?xml?version="1.0"?encoding="utf-8"?>? <resources>?
  2. ????<string?name="hello">Hello?World,?AidlTest!</string>? ????<string?name="app_name">AIDL?Test</string>?
  3. ????<string?name="btn_ok">OK</string>? ????<string?name="btn_cancel">Cancel</string>?
  4. </resources>?

热点排行
Bad Request.