背景
最近Android在用户权限隐私方面,有了更加明确的监管要求。为了配合应用市场需求,我们需要做一些隐私权限管理的处理。比如dialog提示用户需要的权限以及权限的用处,协议明确告知用户隐私的使用,设置里列出各个权限的状态以及修改的入口。在这里就有了今天的笔记,APP里引导用户跳转各个品牌手机的系统权限管理界面。
各品牌的Build.MANUFACTURER
- 小米——Xiaomi
- 华为——Huawei
- oppo——OPPO
- 魅族——Meizu
- 三星——samsung
- 索尼——Sony
- LG——LG
- 乐视——Letv
- 360——QIKU”或“360”
- vivo——vivo
- 酷派——YuLong
- 联想——LENOVO
- 中兴——ZTE
- 谷歌——Google
注:如果不确定大小写,可以Build.MANUFACTURER.toLowerCase()
判断系统制造商,做不同的处理跳转权限管理界面:
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" -> {}//联想
// "zte" -> {}//中兴
// "google" -> {}//谷歌
else -> {
goAppDetailSetting(context)
}
}
}
下面有2个通用的方法,引导跳转到系统设置界面,和跳转到应用信息界面。最后没有处理的这几个系统,和在打开失败的情况下,我的项目中选择默认打开应用信息界面。
系统设置界面
private fun goIntentSetting(context: Context) {
val intent = Intent(Settings.ACTION_SETTINGS)
context.startActivity(intent)
}
应用信息界面
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))
}
}
小米
//小米系统有以下几个版本需要适配
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) {
//如果使用同样的跳转包名类名跳转方式,三星会报这个问题
//java.lang.SecurityException: requires android.permission.GRANT_RUNTIME_PERMISSIONS
//目前选择直接跳转应用信息界面
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)
}
}
乐视
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手机
//360只能打开到自带安全软件
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)
}
}
或许好奇的朋友,你会问这个ComponentName里的参数代表什么,怎么知道的呢?
举个OPPO手机的例子吧
先打开当前应用的系统权限界面,像下图一样:
然后通过ADB命令查看当前activity。可以使用:
adb shell dumpsys activity activities
或者
adb shell dumpsys activity top
第一个参数是权限界面的包名,第二个手机权限界面的类名,既然知道包名与类名自然可以打开该Activity。
ComponentName("com.coloros.safecenter",
"com.coloros.safecenter.permission.PermissionAppAllPermissionActivity")
如果还有疑问,action为何不一样呢?
拿魅族的来看吧:
var intent: Intent = Intent("com.meizu.safe.security.SHOW_APPSEC")
执行命令:adb shell dumpsys activity activities
可以看出Intent的act是什么,如图:
在有限的测试机里,适配测试结果ok。
如果有其他问题,或者有补充的,可以评论或者私信我哟Q-Q
网友评论