Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...