我就拿最近做的项目来说明了,首先创建一个IFMService.aidl的接口文件,在R.java的目录下会自动生成IFMService.java 的文件, 接口文件的内容如下:

package net.kindroid.fm;interface IFMService{/*** open FM*/boolean openFM();/***  close FM*/boolean closeFM();/***  whether FM is open*/boolean isOpen();/***  set a special frequency to the FM*/int setCurrentFrequency(in int frequency);/***  search FM tunes*/boolean searchStation(in int start,in int direction,in int timeout,in int reserve);/***  get current Frequency*/int getCurrentFrequency();/*** stop search FM*/boolean stopSearch();/*** set FM volume*/boolean setVolume(in int value);/***  get current Volume*/int getCurrentVolume();/***  set current mute mode*/int setMuteMode(in int mode);}

public class FMService extends Service:实现如下:

public class MyServiceimpl extends IFMService.Stub{WeakReference<FMService> mService;MyServiceimpl(FMService service){mService = new WeakReference<FMService>(service);}@Overridepublic boolean openFM() throws RemoteException{return mService.get().openFM();}@Overridepublic boolean closeFM() throws RemoteException{return mService.get().closeFM();}@Overridepublic boolean isOpen() throws RemoteException{return mService.get().isOpen();}@Overridepublic boolean searchStation(int start, int direction, int timeout, int reserve)
 throws RemoteException{return mService.get().searchStation(start, direction, timeout, reserve);}@Overridepublic int getCurrentFrequency() throws RemoteException{return mService.get().getTunedFrequency();}@Overridepublic boolean stopSearch() throws RemoteException{return mService.get().stopSearch();}@Overridepublic boolean setVolume(int value) throws RemoteException{return mService.get().setVolume(value);}@Overridepublic int getCurrentVolume() throws RemoteException{return mService.get().getCurrentVolume();}@Overridepublic int setMuteMode(int mode) throws RemoteException{return mService.get().setMuteMode(mode);}@Overridepublic int setCurrentFrequency(int frequency) throws RemoteException{return mService.get().setCurrentFrequency(frequency);}}


在service里面实现get().的那些方法即可在当前app里面使用这个service了

那么如何在其他app里面使用到这个service呢?

在要使用上面服务的那个app的src目录下面新建一个目录树,要和IFMService的package相对应,比如本例的net.kindroid.fm,然后把生成后的IFMService.java文件拷贝到新建的这个目录树下面,那么在代码里面就可以使用了:

mContext.startService(new Intent("net.kindroid.aidl.service.IFMService"));mContext.bindService(new Intent("net.kindroid.aidl.service.IFMService"),serviceConnection, Context.BIND_AUTO_CREATE);

使用的方法和service的那个app类似,就可以调用我们刚刚写的那个service了,还是贴上使用的方法吧:

if (mFMService == null){try{// start serverthis.startService(new Intent("net.kindroid.aidl.service.IFMService"));bindService(new Intent("net.kindroid.aidl.service.IFMService"),
 serviceConnection, Context.BIND_AUTO_CREATE);} catch (Exception e){}}


/*** fm service*/private IFMService mFMService = null;
/*** fm service connection*/private ServiceConnection serviceConnection = new ServiceConnection(){@Overridepublic void onServiceDisconnected(ComponentName name){mFMService = null;}@Overridepublic void onServiceConnected(ComponentName name, IBinder service){mFMService = IFMService.Stub.asInterface(service);if (hasData()){SharedPreferences settings = getSharedPreferences(FMRADIO_DATA, 0);int current = settings.getInt(CHANNEL_VALUE, 1017);if (digitView != null){try{if (mFMService != null && !mFMService.isOpen()){if (!FMService.isMusicVolumeFocus && !isCallState()){mFMService.openFM();play.setBackgroundResource(R.drawable.stop);setEnabled(true);setChannelToPlay(current);setVolume();} else{digitView.setChannel(current);updateRedLine(current);}} else{digitView.setChannel(current);updateRedLine(current);}} catch (Exception e){}}}}};


这样就实现了进程间的服务通讯了,具体的底层机制呢,大家研究一下binder机制吧,呵呵

更多相关文章

  1. Multidex Android(安卓)DEX手动拆包
  2. android源码中常用的Rect方法
  3. Android第一行代码学习笔记Chapter5&6
  4. 关于编译Android时出现Error 41的解决方法
  5. ubuntu 9.04使用adb调试Android方法
  6. Android(安卓)高通代码预制apk可卸载,恢复出厂设置apk可恢复 Andr
  7. cordova build android提示No installed build tools found的解
  8. Android(安卓)解决 GetLastKnownLocation(provider) = null
  9. android fragment 重复创建的问题

随机推荐

  1. android NDK 开发
  2. android读取SDCard任意路径下的文件
  3. Android(安卓)读取XML的两种方法。
  4. Android(安卓)倒影实现算法
  5. Ubuntu 12.04 Desktop 版本编译 Android(
  6. Android(安卓)SDK无法更新终极解决方式
  7. Android(安卓)SQLite数据库解析并使用两
  8. 【Android-Third】Android三方框架相关
  9. How to generate links to the android C
  10. java.util.concurrent.ExecutionExceptio