首頁 > 資料庫 > mysql教程 > 如何给Service添加access permission

如何给Service添加access permission

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
發布: 2016-06-07 15:50:13
原創
1261 人瀏覽過

//首先定义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");
        }
        //做实际的工作

    }

}

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
如何學習MongoDB?
來自於 1970-01-01 08:00:00
0
0
0
如何配置 bcmath
來自於 1970-01-01 08:00:00
0
0
0
java如何產生token?
來自於 1970-01-01 08:00:00
0
0
0
phpstudy 自備apache如何升級openssl到1.0.2o版本
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板