Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open app permission Settings in MIUI devices programmatically?

I want to open the screen like this: Settings -> Apps -> Select an app -> Open App permissions screen.

Currently I used this code. It lets me open the AppInfo screen. But I don't know how to select the App permissions screen.

                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", APP_PACKAGE_NAME, null);
                intent.setData(uri);
                startActivity(intent);

Some devices running MIUI have the app permissions screen.

like image 822
TOP Avatar asked Mar 15 '23 02:03

TOP


2 Answers

Just add this code and call applyMiuiPermission() method

private static final String TAG = "MiuiUtils";

public static void applyMiuiPermission(Context context) {
    int versionCode = getMiuiVersion();
    if (versionCode == 5) {
        goToMiuiPermissionActivity_V5(context);
    } else if (versionCode == 6) {
        goToMiuiPermissionActivity_V6(context);
    } else if (versionCode == 7) {
        goToMiuiPermissionActivity_V7(context);
    } else {
        Log.e(TAG, "this is a special MIUI rom version, its version code " + versionCode);
    }
}


public static int getMiuiVersion() {
    String version = RomUtils.getSystemProperty("ro.miui.ui.version.name");
    if (version != null) {
        try {
            return Integer.parseInt(version.substring(1));
        } catch (Exception e) {
            Log.e(TAG, "get miui version code error, version : " + version);
        }
    }
    return -1;
}

public static void goToMiuiPermissionActivity_V5(Context context) {
    Intent intent = null;
    String packageName = context.getPackageName();
    intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    Uri uri = Uri.fromParts("package" , packageName, null);
    intent.setData(uri);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (isIntentAvailable(intent, context)) {
        context.startActivity(intent);
    } else {
        Log.e(TAG, "intent is not available!");
    }
}


public static void goToMiuiPermissionActivity_V6(Context context) {
    Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
    intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
    intent.putExtra("extra_pkgname", context.getPackageName());
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    if (isIntentAvailable(intent, context)) {
        context.startActivity(intent);
    } else {
        Log.e(TAG, "Intent is not available!");
    }
}


public static void goToMiuiPermissionActivity_V7(Context context) {
    Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
    intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
    intent.putExtra("extra_pkgname", context.getPackageName());
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    if (isIntentAvailable(intent, context)) {
        context.startActivity(intent);
    } else {
        Log.e(TAG, "Intent is not available!");
    }
}

RomUtils.Java

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RomUtils {
    private static final String TAG = "RomUtils";

    public static boolean checkIsMiuiRom() {
        return !TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name"));
    }
    public static String getSystemProperty(String propName) {
        String line;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            Log.e(TAG, "Unable to read sysprop " + propName, ex);
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    Log.e(TAG, "Exception while closing InputStream", e);
                }
            }
        }
        return line;
    }

    public static boolean checkIsMeizuRom() {
        return Build.MANUFACTURER.contains("Meizu");
    }
}
like image 110
Kunjeti Aswarthamaiah Gupta Avatar answered Apr 26 '23 21:04

Kunjeti Aswarthamaiah Gupta


Try this solution

Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
intent.putExtra("extra_pkgname", "app_package_name");
like image 26
Zakharov Roman Avatar answered Apr 26 '23 23:04

Zakharov Roman