我就拿最近做的項目來說明了,首先創建一個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);
- }
-
- @Override
- public boolean openFM() throws RemoteException
- {
- return mService.get().openFM();
- }
-
- @Override
- public boolean closeFM() throws RemoteException
- {
- return mService.get().closeFM();
- }
-
- @Override
- public boolean isOpen() throws RemoteException
- {
- return mService.get().isOpen();
- }
-
- @Override
- public boolean searchStation(int start, int direction, int timeout, int reserve) throws RemoteException
- {
- return mService.get().searchStation(start, direction, timeout, reserve);
- }
-
- @Override
- public int getCurrentFrequency() throws RemoteException
- {
- return mService.get().getTunedFrequency();
- }
-
- @Override
- public boolean stopSearch() throws RemoteException
- {
- return mService.get().stopSearch();
- }
-
- @Override
- public boolean setVolume(int value) throws RemoteException
- {
- return mService.get().setVolume(value);
- }
-
- @Override
- public int getCurrentVolume() throws RemoteException
- {
- return mService.get().getCurrentVolume();
- }
-
- @Override
- public int setMuteMode(int mode) throws RemoteException
- {
- return mService.get().setMuteMode(mode);
- }
-
- @Override
- public int setCurrentFrequency(int frequency) throws RemoteException
- {
- return mService.get().setCurrentFrequency(frequency);
- }
- }