背景
最近Android在用戶權(quán)限隱私方面候衍,有了更加明確的監(jiān)管要求蛇尚。為了配合應(yīng)用市場(chǎng)需求摆碉,我們需要做一些隱私權(quán)限管理的處理塘匣。比如dialog提示用戶需要的權(quán)限以及權(quán)限的用處,協(xié)議明確告知用戶隱私的使用巷帝,設(shè)置里列出各個(gè)權(quán)限的狀態(tài)以及修改的入口忌卤。在這里就有了今天的筆記,APP里引導(dǎo)用戶跳轉(zhuǎn)各個(gè)品牌手機(jī)的系統(tǒng)權(quán)限管理界面楞泼。
各品牌的Build.MANUFACTURER
- 小米——Xiaomi
- 華為——Huawei
- oppo——OPPO
- 魅族——Meizu
- 三星——samsung
- 索尼——Sony
- LG——LG
- 樂(lè)視——Letv
- 360——QIKU”或“360”
- vivo——vivo
- 酷派——YuLong
- 聯(lián)想——LENOVO
- 中興——ZTE
- 谷歌——Google
注:如果不確定大小寫(xiě)埠巨,可以Build.MANUFACTURER.toLowerCase()
判斷系統(tǒng)制造商,做不同的處理跳轉(zhuǎn)權(quán)限管理界面:
fun jumpPermissionPage(context: Context) {
val manufacturer = DeviceUtils.getManufacturer().toLowerCase()
when (manufacturer) {
"xiaomi" -> {
goXiaoMiManager(context)
}
"huawei" -> {
goHuaWeiManager(context)
}
"oppo" -> {
goOppoManager(context)
}
"meizu" -> {
goMeizuManager(context)
}
"samsung" -> {
goSamsungManager(context)
}
"sony" -> {
goSonyManager(context)
}
"lg" -> {
goLGManager(context)
}
"letv" -> {
goLetvManager(context)
}
"qiku", "360" -> {
go360Manager(context)
}
// "vivo" -> {}//vivo
// "yulong" -> {}//酷派
// "lenovo" -> {}//聯(lián)想
// "zte" -> {}//中興
// "google" -> {}//谷歌
else -> {
goAppDetailSetting(context)
}
}
}
下面有2個(gè)通用的方法现拒,引導(dǎo)跳轉(zhuǎn)到系統(tǒng)設(shè)置界面辣垒,和跳轉(zhuǎn)到應(yīng)用信息界面。最后沒(méi)有處理的這幾個(gè)系統(tǒng)印蔬,和在打開(kāi)失敗的情況下勋桶,我的項(xiàng)目中選擇默認(rèn)打開(kāi)應(yīng)用信息界面。
系統(tǒng)設(shè)置界面
private fun goIntentSetting(context: Context) {
val intent = Intent(Settings.ACTION_SETTINGS)
context.startActivity(intent)
}
應(yīng)用信息界面
private fun goAppDetailSetting(context: Context) {
val localIntent = Intent()
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
if (Build.VERSION.SDK_INT >= 9) {
localIntent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
localIntent.data = Uri.fromParts("package", BuildConfig.APPLICATION_ID, null)
} else if (Build.VERSION.SDK_INT <= 8) {
localIntent.action = Intent.ACTION_VIEW
localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails")
localIntent.putExtra("com.android.settings.ApplicationPkgName", BuildConfig.APPLICATION_ID)
}
try {
context.startActivity(localIntent)
} catch (e: Exception) {
// e.printStackTrace()
AppUtils.topActivity?.showTip(context.resources.getString(R.string.permission_setting_error))
}
}
小米
//小米系統(tǒng)有以下幾個(gè)版本需要適配
private fun goXiaoMiManager(context: Context) {
var rom: String? = getMiuiVersion()
try {
var intent: Intent = Intent()
if ("V5" == rom) {
val packageURI = Uri.parse("package:" + BuildConfig.APPLICATION_ID)
intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI)
} else if ("V6" == rom || "V7" == rom) {
intent.action = "miui.intent.action.APP_PERM_EDITOR"
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity")
intent.putExtra("extra_pkgname", BuildConfig.APPLICATION_ID)
} else if ("V8" == rom || "V9" == rom) {
intent.action = "miui.intent.action.APP_PERM_EDITOR"
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity")
intent.putExtra("extra_pkgname", BuildConfig.APPLICATION_ID)
} else {
goAppDetailSetting(context)
}
context.startActivity(intent)
} catch (e: Exception) {
goAppDetailSetting(context)
}
}
華為
private fun goHuaWeiManager(context: Context) {
try {
var intent: Intent = Intent(BuildConfig.APPLICATION_ID)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
var comp: ComponentName = ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity")
intent.setComponent(comp)
context.startActivity(intent)
} catch (e: Exception) {
goAppDetailSetting(context)
}
}
OPPO
private fun goOppoManager(context: Context) {
try {
val intent = Intent("android.settings.APPLICATION_DETAILS_SETTINGS")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("packageName", BuildConfig.APPLICATION_ID)
// val comp = ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.PermissionAppAllPermissionActivity")//R9SK 6.0.1 os-v3.0
val comp = ComponentName("com.coloros.securitypermission", "com.coloros.securitypermission.permission.PermissionAppAllPermissionActivity")//R11t 7.1.1 os-v3.2
intent.component = comp
context.startActivity(intent)
}catch (e: Exception) {
goAppDetailSetting(context)
}
}
魅族
private fun goMeizuManager(context: Context) {
try {
var intent: Intent = Intent("com.meizu.safe.security.SHOW_APPSEC")
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.putExtra("packageName", BuildConfig.APPLICATION_ID)
context.startActivity(intent)
} catch (e: Exception) {
goAppDetailSetting(context)
}
}
三星
private fun goSamsungManager(context: Context) {
//如果使用同樣的跳轉(zhuǎn)包名類名跳轉(zhuǎn)方式侥猬,三星會(huì)報(bào)這個(gè)問(wèn)題
//java.lang.SecurityException: requires android.permission.GRANT_RUNTIME_PERMISSIONS
//目前選擇直接跳轉(zhuǎn)應(yīng)用信息界面
goAppDetailSetting(context)
}
索尼
private fun goSonyManager(context: Context) {
try {
var intent: Intent = Intent(BuildConfig.APPLICATION_ID)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
var comp: ComponentName = ComponentName("com.sonymobile.cta", "com.sonymobile.cta.SomcCTAMainActivity")
intent.setComponent(comp)
context.startActivity(intent)
} catch (e: Exception) {
goAppDetailSetting(context)
}
}
LG
private fun goLGManager(context: Context) {
try {
val intent = Intent("android.intent.action.MAIN")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("packageName", BuildConfig.APPLICATION_ID)
val comp = ComponentName("com.android.settings", "com.android.settings.Settings\$AccessLockSummaryActivity")
intent.component = comp
context.startActivity(intent)
}catch (e: Exception) {
goAppDetailSetting(context)
}
}
樂(lè)視
private fun goLetvManager(context: Context) {
try {
val intent = Intent()
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("packageName", BuildConfig.APPLICATION_ID)
val comp = ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.PermissionAndApps")
intent.component = comp
context.startActivity(intent)
}catch (e: Exception) {
goAppDetailSetting(context)
}
}
360手機(jī)
//360只能打開(kāi)到自帶安全軟件
private fun go360Manager(context: Context) {
try {
val intent = Intent("android.intent.action.MAIN")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("packageName", BuildConfig.APPLICATION_ID)
val comp = ComponentName("com.qihoo360.mobilesafe", "com.qihoo360.mobilesafe.ui.index.AppEnterActivity")
intent.component = comp
context.startActivity(intent)
}catch (e: Exception) {
goAppDetailSetting(context)
}
}
或許好奇的朋友例驹,你會(huì)問(wèn)這個(gè)ComponentName里的參數(shù)代表什么,怎么知道的呢退唠?
舉個(gè)OPPO手機(jī)的例子吧
先打開(kāi)當(dāng)前應(yīng)用的系統(tǒng)權(quán)限界面鹃锈,像下圖一樣:
然后通過(guò)ADB命令查看當(dāng)前activity∏圃ぃ可以使用:
adb shell dumpsys activity activities
或者
adb shell dumpsys activity top
第一個(gè)參數(shù)是權(quán)限界面的包名屎债,第二個(gè)手機(jī)權(quán)限界面的類名,既然知道包名與類名自然可以打開(kāi)該Activity垢油。
ComponentName("com.coloros.safecenter",
"com.coloros.safecenter.permission.PermissionAppAllPermissionActivity")
如果還有疑問(wèn)盆驹,action為何不一樣呢?
拿魅族的來(lái)看吧:
var intent: Intent = Intent("com.meizu.safe.security.SHOW_APPSEC")
執(zhí)行命令:adb shell dumpsys activity activities
可以看出Intent的act是什么滩愁,如圖:
在有限的測(cè)試機(jī)里躯喇,適配測(cè)試結(jié)果ok。
如果有其他問(wèn)題硝枉,或者有補(bǔ)充的廉丽,可以評(píng)論或者私信我喲Q-Q