
//首先定義Service的接口,IMyService.aidl package com.xxxx; import android.os.IBinder; import android.os.ParcelFileDescriptor; interface IMyService { void doService(int id); } //實(shí)現(xiàn)service類(lèi),MyService.java public class MyService extends IM
//首先定義Service的接口,IMyService.aidl
package com.xxxx;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
interface IMyService {
void doService(int id);
}
//實(shí)現(xiàn)service類(lèi),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 {
//添加訪問(wèn)權(quán)限
if (mContext.checkCallingPermission(PERMISSION) != PackageManager.PERMISSION_GRANTED) {
throw new RemoteException("Permission not granted for MyService");
}
//做實(shí)際的工作
}
}
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com