How to start activity from background in android 10 - android Q - MIUI 11

爱⌒轻易说出口 提交于 2021-02-08 12:54:51

问题


I have problem with starting activity from background in android 10 - android Q - MIUI 11 on real device. In this thread: start activity background in android 10 I found information how to do this on Android 10 and everything working perfectly on emulator.

I tested this on Xiaomi Mi 9T Pro with Android 10 (MIUI 11) and seems that this doesn't work.

In logcat I have following logs:

-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

Why this not working on xiaomi device?

Edit:

I have found solution. In MIUI there is additional permission which should be turned on. It's called: Display pop-up windows while running in the background. This is code to show window with this permission:

First I'm checking if user have MIUI installed on phone:

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;
}

If yes then window with permissions can be shown:

 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);
}

Now I need to check if user granted this permission because I don't want to show this window every time when app is started. Anyone know how to do this?

来源:https://stackoverflow.com/questions/59974951/how-to-start-activity-from-background-in-android-10-android-q-miui-11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!