First, define the Service interface, IMyService. aidlpackagecom. xxxx; importandroid. OS. IBinder; importandroid. OS. ParcelFileDescriptor; interfaceIMyService {voiddoService (intid);} implements the service class, MyService. Example
// First define the Service interface, IMyService. aidl package com. xxxx; import android. OS. IBinder; import android. OS. parcelFileDescriptor; interface IMyService {void doService (int id);} // implements the service class, MyService. java public class MyService extends IM
// First define the Service interface, IMyService. aidl
Package com. xxxx;
Import android. OS. IBinder;
Import android. OS. ParcelFileDescriptor;
Interface IMyService {
Void doService (int id );
}
// Implement the service class, MyService. java
Public class MyService extends IMyService. Stub {
Public static final String MY_SERVICE = "myservice ";
Private static MyService sService = null;
Static String PERMISSION = "com. xxxx. permission. ACCESS_MYSERVICE ";
/*
* The entry called by system server to create service.
*/
Public static MyService main (Context context ){
If (sService! = Null ){
Return sService;
}
SService = new MyService (context );
Try {
Slog. d (TAG, "created service ");
ServiceManager. addService (MY_SERVICE, sService );
Slog. d (TAG, "added service" + MY_SERVICE );
} Catch (Throwable e ){
Slog. e (TAG, "Failure starting MyService", e );
}
Return sService;
}
Private MyService (Context context ){
MContext = context;
}
/*
* Called from Client App to retrieve interface
*/
Public static IMyService getService (){
IBinder B = ServiceManager. getService (MY_SERVICE );
If (B = null ){
Return null;
}
Return IMyService. Stub. asInterface (B );
}
@ Override
Public long openSession (IBinder clientToken, int sensorType) throws RemoteException {
// Add Access Permissions
If (mContext. checkCallingPermission (PERMISSION )! = PackageManager. PERMISSION_GRANTED ){
Throw new RemoteException ("Permission not granted for MyService ");
}
// Do the actual work
}
}