Android 访问设置时出现安全异常
Android security exception when accessing settings
我需要从我的应用程序中打开使用情况统计设置。对于大多数手机,一切正常:
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
但是,有些手机如华为Y6 Pro会抛出安全异常java.lang.SecurityException: Permission Denial: starting Intent { act=android.settings.USAGE_ACCESS_SETTINGS cmp=com.android.settings/.Settings$UsageAccessSettingsActivity } from ProcessRecord{3f032f8b 18712:com.example.pro/u0a924} (pid=18712, uid=10924) not exported from uid 1000
有没有办法检查我是否可以执行 startActivity
并且它不会抛出任何异常?
已更新
试试这个:
将此添加到你 activity 的清单中
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
我想你可以检查是否可以使用这个
执行以下意图
科特林:
fun canPerformIntent(context: Context, intent: Intent): Boolean {
val mgr = context.packageManager
val list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
return list.size > 0
}
Java:
public static boolean canPerformIntent(Context context, Intent intent) {
PackageManager mgr = context.getPackageManager();
List<ResolveInfo> list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
并像这样使用
if (canPerformIntent(this, Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS))) {
//TODO: start the intent
}
我需要从我的应用程序中打开使用情况统计设置。对于大多数手机,一切正常:
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
但是,有些手机如华为Y6 Pro会抛出安全异常java.lang.SecurityException: Permission Denial: starting Intent { act=android.settings.USAGE_ACCESS_SETTINGS cmp=com.android.settings/.Settings$UsageAccessSettingsActivity } from ProcessRecord{3f032f8b 18712:com.example.pro/u0a924} (pid=18712, uid=10924) not exported from uid 1000
有没有办法检查我是否可以执行 startActivity
并且它不会抛出任何异常?
已更新
试试这个:
将此添加到你 activity 的清单中
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
我想你可以检查是否可以使用这个
执行以下意图科特林:
fun canPerformIntent(context: Context, intent: Intent): Boolean {
val mgr = context.packageManager
val list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
return list.size > 0
}
Java:
public static boolean canPerformIntent(Context context, Intent intent) {
PackageManager mgr = context.getPackageManager();
List<ResolveInfo> list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
并像这样使用
if (canPerformIntent(this, Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS))) {
//TODO: start the intent
}