6

How to get Permission for read "Service SMS" in MIUI 8+ (programmatically).

How to get Permission for read "Service SMS" in MIUI 8+ (programmatically)

Micho
  • 3,929
  • 13
  • 37
  • 40
Sagar Makhija
  • 863
  • 8
  • 9
  • runtime permissions https://stackoverflow.com/questions/38141523/directory-creation-not-working-in-marshmallow-android/38141778#38141778 – Sohail Zahid Jul 11 '17 at 11:11
  • 1
    I can read sms of person or mobile (i have that system android permission). but the problem is i cant read service sms (Only in MiUi 8.0+) for eg : i got sms from some otp service like paytm, the sender is 'PayTM' its not a mobile no so it goes to sms/inbox/notification or may b sms/notification if i want to read service sms i have to allow service sms permission under app -> other permissions -> service sms -> allow. – Sagar Makhija Jul 11 '17 at 11:22

1 Answers1

8

This will launch the intent for service sms. Once user will allow the access for service sms you will able to read the notification sms.

if (isMIUI()) {
            //this will launch the auto start screen where user can enable the permission for your app
      Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
                    localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
      localIntent.putExtra("extra_pkgname", getActivity().getPackageName());
      startActivity(localIntent);
}


 public static boolean isMIUI() {
        String device = Build.MANUFACTURER;
        if (device.equals("Xiaomi")) {
            try {
                Properties prop = new Properties();
                prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
                return prop.getProperty("ro.miui.ui.version.code", null) != null
                        || prop.getProperty("ro.miui.ui.version.name", null) != null
                        || prop.getProperty("ro.miui.internal.storage", null) != null;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

         return false;
  }

Note: you can not take the permission programmatically it's only allowed for whitelisted app from MIUI. for example- facebook messenger, whatsapp, flipkart etc have autostart option by default.

Sonu Kumar
  • 91
  • 1
  • 8
  • 1
    Can we check whether a user has already given this permission or not? Because it will always call the `localIntent` which will open the permission setting even If user has given the permission. Any idea how to solve this problem? – Jay Patel Oct 14 '18 at 08:52
  • @JayPatel I did not got any solution for this but yes I can suggest a way by which you will be able to figure out. You can read the SMS 1st and see if are you able to read the services SMS or not, if Yes no need to ask again, but if no then you can ask it again. – Sonu Kumar Oct 15 '18 at 06:30
  • Humm, Sounds good solution, but how can I identify whether 1st SMS is a service message or regular message? – Jay Patel Oct 15 '18 at 06:45
  • check the address of the SMS if it's 10 digit number then it's regular one but if it's not then it's service sms. For example if it's a regular SMS then address will be +917042XXXX73, If it's services SMS the address will HPFPAXXA. – Sonu Kumar Oct 15 '18 at 07:10