Android MIUI Notification SMS Permission Pit

  • 2021-08-21 21:33:40
  • OfStack

There are quite a few cheating designs in MIUI. For example, MIUI mobile phone can't debug and install USB without inserting SIM card. OK, insert it, and then let you log in to Xiaomi account first (nothing to say). MIUI permission application is also a pit!

Take the permission of READ_SMS, which is dynamically applied according to Android specification. It does not pop up the window for users to allow, but then the callback is successful. Look at the setting 1, and the permission is still in the inquiry status. What logic is this!
Then I want to automatically fill in the SMS verification code, which is not easy to realize. I have customized a notification SMS permission (Service_SMS) in MIUI. You don't know how to apply or register in manifest. So you have no right, and you are limited to what you can't listen to. I also don't know what the complete name of this permission is. There are only two states in the settings (without asking, it is estimated that there is no dynamic application).

At present, there is a solution that you guide the user to open it, but now SMS is in the form of notification bar, and there will be a copy button, which is actually one more move, but if it is other sensitive permissions, it may be valuable to do so. As follows:

1, first judging whether the system is MIUI, and then calling goPermissionSettings (Activity activity) at the required Activity;

2. Rewrite onActivityResult in the corresponding activity, and according to request_code, the callback will directly execute the business code that needs permission. However, it is impossible to judge whether the user has given permission or not. He can only "blindly tune" (a word created by himself) the code that needs permission, so he needs try/catch1. If it collapses, it means that there is no permission, and if there is no problem, it means that he has given permission.

3. You can use sharePreference to record the authorization status without booting every time (but it may still happen that the user gives permission and then turns it off manually). The above try/catch is very important ah, catch can do some processing, boot again or you have other ideas.


public class MiuiUtils {

  private MiuiUtils() {
    throw new UnsupportedOperationException("u can't instantiate me...");
  }

  private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
  private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
  private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
  public static final int REQUEST_CODE_SERVICE_SMS = 100;


  /**
   * @return whether or not is MIUI
   * @link http://dev.xiaomi.com/doc/p=254/index.html
   */
  public static boolean isMIUI() {
    String device = Build.MANUFACTURER;
    LogUtils.v("Build.MANUFACTURER = " + device);
    if (device.equals("Xiaomi")) {
      Properties prop = new Properties();
      try {
        prop.load(new FileInputStream(new File(Environment
            .getRootDirectory(), "build.prop")));
      } catch (IOException e) {
        e.printStackTrace();
        return false;
      }
      return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
          || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
          || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
    } else {
      return false;
    }
  }


  public static void goPermissionSettings(Activity context) {
    Intent intent;
    try {//MIUI8/9
      intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
      intent.setClassName("com.miui.securitycenter",
          "com.miui.permcenter.permissions.PermissionsEditorActivity");
      intent.putExtra("extra_pkgname", context.getPackageName());
      context.startActivityForResult(intent, REQUEST_CODE_SERVICE_SMS);
    } catch (ActivityNotFoundException e) {
      try {//MIUI5/6
        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());
        context.startActivityForResult(intent, REQUEST_CODE_SERVICE_SMS);
      } catch (ActivityNotFoundException e1) {
        // Application information interface 
        intent = new Intent(
            Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", context.getPackageName(),
            null);
        intent.setData(uri);
        context.startActivityForResult(intent, REQUEST_CODE_SERVICE_SMS);;
      }
    }
  }
}

Give a useful ADB command. Can get the current Activity information, otherwise you think, is how to know the above goPermissionSettings (Activity activity) method, jump to the MIUI permission set Activity package name and full class name:


adb shell dumpsys activity | findstr "mFocusedActivity"

Related articles: