Android系統開發 AppOpsManager 應用權限管理

前言

   AppOpsManager是應用權限管理器,負責控制應用權限設置。appops是在現有權限機制上新增的一套權限管理機制,主要針對一些高危的非必須系統應用的權限。 請注意!調用AppOpsManager需要系統級權限。

 

檢查與設置權限

/**
 * 檢查權限
 * [op]這個參數請參考 AppOpsManager.OPSTR_CAMERA 這些常量
 * 返回值請參考 AppOpsManager.MODE_ALLOWED 這些常量
 */
fun checkPermissions(context: Context, packageName: String, op: String): Int {
    val uid: Int = context.getPackageManager().getPackageUid(packageName, 0)
    val appOpsManager = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
    val result = appOpsManager.checkOpNoThrow(op, uid, packageName)
    return result
}

/**
 * 設置權限
 * [op]這個參數請參考 AppOpsManager.OPSTR_CAMERA 這些常量
 * [mode]這個參數請參考 AppOpsManager.MODE_ALLOWED 這些常量
 */
fun setPermissions(context: Context, packageName: String, op: String, mode:Int) {
    val uid: Int = context.getPackageManager().getPackageUid(packageName, 0)
    val appOpsManager = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
    appOpsManager.setMode(op, uid, packageName, mode)
}

上面的代碼使用例子

//檢查權限
val permissionsState = PermissionsHelp.checkPermissions(this, "xxx.xxx.xxx", AppOpsManager.OPSTR_CAMERA)
//設置權限
PermissionsHelp.setPermissions(this, "xxx.xxx.xxx", AppOpsManager.OPSTR_CAMERA, AppOpsManager.MODE_ALLOWED)

mode參數

/**
 * 允許權限
 */
public static final int MODE_ALLOWED = 0;

/**
 * 忽略權限
 */
public static final int MODE_IGNORED = 1;

/**
 * 拒絕權限,並且會引起報錯
 */
public static final int MODE_ERRORED = 2;

/**
 * 默認,通常不使用這種模式;它應該只在appop權限下使用,調用者必須顯式地檢查它並處理它。
 */
public static final int MODE_DEFAULT = 3;

/**
 * 特殊模式只允許應用處於前臺時,當設置此模式時,當被檢查的應用程序當前處於前臺時返回{@link MODE_ALLOWED},否則返回{@link MODE_IGNORED}。
 * @hide
 */
public static final int MODE_FOREGROUND = 4;

可申請的權限列表

/** 訪問粗略的位置信息。*/
public static final String OPSTR_COARSE_LOCATION = "android:coarse_location";
/** 訪問精細的位置信息。*/
public static final String OPSTR_FINE_LOCATION ="android:fine_location";
/** 持續監控位置數據。*/
public static final String OPSTR_MONITOR_LOCATION = "android:monitor_location";
/** 在相對較高的功率要求下持續監控位置數據。*/
public static final String OPSTR_MONITOR_HIGH_POWER_LOCATION = "android:monitor_location_high_power";
/** 訪問統計數據權限 */
public static final String OPSTR_GET_USAGE_STATS = "android:get_usage_stats";
/** 激活VPN連接,無需用戶干預 */
public static final String OPSTR_ACTIVATE_VPN = "android:activate_vpn";
/** 允許應用程序讀取用戶的聯繫人數據. */
public static final String OPSTR_READ_CONTACTS = "android:read_contacts";
/** 允許應用程序寫入用戶的聯繫人數據. */
public static final String OPSTR_WRITE_CONTACTS = "android:write_contacts";
/** 允許程序讀取用戶的通話記錄. */
public static final String OPSTR_READ_CALL_LOG = "android:read_call_log";
/** 允許應用程序寫入用戶的呼叫記錄. */
public static final String OPSTR_WRITE_CALL_LOG = "android:write_call_log";
/** 允許應用程序讀取用戶的日曆數據. */
public static final String OPSTR_READ_CALENDAR = "android:read_calendar";
/** 允許應用程序寫入用戶的日曆數據. */
public static final String OPSTR_WRITE_CALENDAR = "android:write_calendar";
/** 允許應用程序發起一個電話呼叫. */
public static final String OPSTR_CALL_PHONE = "android:call_phone";
/** 允許程序讀取短信. */
public static final String OPSTR_READ_SMS = "android:read_sms";
/** 允許應用程序接收短信. */
public static final String OPSTR_RECEIVE_SMS = "android:receive_sms";
/** 允許應用程序接收彩信. */
public static final String OPSTR_RECEIVE_MMS = "android:receive_mms";
/** 允許應用程序接收WAP推送消息. */
public static final String OPSTR_RECEIVE_WAP_PUSH = "android:receive_wap_push";
/** 允許應用程序發送短信. */
public static final String OPSTR_SEND_SMS = "android:send_sms";
/** 需要能夠訪問攝像設備. */
public static final String OPSTR_CAMERA = "android:camera";
/** 需要能夠訪問麥克風設備. */
public static final String OPSTR_RECORD_AUDIO = "android:record_audio";
/** 需要訪問電話狀態相關信息. */
public static final String OPSTR_READ_PHONE_STATE = "android:read_phone_state";
/** 需要訪問電話狀態相關信息. */
public static final String OPSTR_ADD_VOICEMAIL = "android:add_voicemail";
/** 通過VOIP或WiFi訪問SIP呼叫的api */
public static final String OPSTR_USE_SIP = "android:use_sip";
/** 訪問用於轉移呼出的api */
public static final String OPSTR_PROCESS_OUTGOING_CALLS = "android:process_outgoing_calls";
/** 使用指紋API. */
public static final String OPSTR_USE_FINGERPRINT = "android:use_fingerprint";
/** 接入身體傳感器,如心率等. */
public static final String OPSTR_BODY_SENSORS = "android:body_sensors";
/** 讀取先前收到的蜂窩廣播消息. */
public static final String OPSTR_READ_CELL_BROADCASTS = "android:read_cell_broadcasts";
/** 將模擬位置注入系統. */
public static final String OPSTR_MOCK_LOCATION = "android:mock_location";
/** 讀取外部存儲器. */
public static final String OPSTR_READ_EXTERNAL_STORAGE = "android:read_external_storage";
/** 寫入外部存儲器. */
public static final String OPSTR_WRITE_EXTERNAL_STORAGE = "android:write_external_storage";
/** 需要在其他應用程序上繪圖. */
public static final String OPSTR_SYSTEM_ALERT_WINDOW = "android:system_alert_window";
/** 需要寫入、修改、更新系統設置. */
public static final String OPSTR_WRITE_SETTINGS = "android:write_settings";

 

待續補充...

 

 

end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章