我在实际设备上从android 10-android Q-MIUI 11中的后台启动活动时遇到问题。在此线程中:start activity background in android 10我找到了如何在Android 10上执行此操作以及在模拟器上一切正常运行的信息。
我在装有Android 10(MIUI 11)的小米Mi 9T Pro上进行了测试,似乎无法正常工作。
在logcat中,我有以下日志:
-W/ActivityTaskManager: Background activity start for com.com.app allowed because SYSTEM_ALERT_WINDOW permission is granted.
-D/ActivityTaskManagerServiceInjector: MIUILOG- Permission Denied Activity : Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.android.chrome cmp=com.android.chrome/com.google.android.apps.chrome.Main } pkg : com.com.app uid : 10283 tuid : 10052
为什么这在小米设备上不起作用?
编辑:
我已经找到解决方案。在MIUI中,应启用其他权限。称为:在后台运行时显示弹出窗口。这是显示具有此权限的窗口的代码:
首先,我正在检查用户是否在手机上安装了MIUI:
public static boolean isMiUi() {
return !TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name"));
}
public static String getSystemProperty(String propName) {
String line;
BufferedReader input = null;
try {
java.lang.Process p = Runtime.getRuntime().exec("getprop " + propName);
input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
line = input.readLine();
input.close();
} catch (IOException ex) {
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return line;
}
如果是,则可以显示具有权限的窗口:
private void showPermissionsDialog() {
Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
intent.setClassName("com.miui.securitycenter",
"com.miui.permcenter.permissions.PermissionsEditorActivity");
intent.putExtra("extra_pkgname", getPackageName());
startActivity(intent);
}
现在,我需要检查用户是否已授予此权限,因为我不想每次启动应用程序时都显示此窗口。有人知道该怎么做吗?