欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

如何给Service添加access permission

程序员文章站 2024-01-04 11:50:10
...

//首先定义Service的接口,IMyService.aidl package com.xxxx; import android.os.IBinder; import android.os.ParcelFileDescriptor; interface IMyService { void doService(int id); } //实现service类,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);
}
//实现service类,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 {

//添加访问权限

if (mContext.checkCallingPermission(PERMISSION) != PackageManager.PERMISSION_GRANTED) {
throw new RemoteException("Permission not granted for MyService");
}
//做实际的工作

}

}

上一篇:

下一篇: