MIUI 10 doesn't let service start activity - Xiaomi Redmi Note
Asked Answered
P

1

26

My app has a service and an activity. From the service, activity is called with following code:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

even without the flags, normally the activity window is displayed with its correct layout. However, on Xiaomi Redmi Note 4 with Android 7, activity layout is not displayed. I only see the following line on logcat:

I/Timeline: Timeline: Activity_launch_request time:281438674 intent:Intent { flg=0x30000000 cmp=com.test.app/.MainActivity }

I believe this is not an Android 7 (API 24) issue because on another device with Android 7, service can successfully start the activity. I guess, MIUI is preventing the launch of the activity from service.

I tried changing how the activity was defined in the manifest. I also tried with several different flags. All of my tests failed. I could not succeed in starting the activity. Worst issue is that there is no error/exception in the logs.

Any ideas on this please ?

Perithecium answered 8/12, 2019 at 19:23 Comment(2)
Same problem on MIUI 11.Gennygeno
a fix can found in #59419004 has todo with Google play permissions and target SDKsOctodecillion
L
33

In App Info screen from the system Settings app > Other permissions > Display pop-up windows while running in the background.

This seems introduced in MIUI 11.

Edit: here is a piece of code that I use. I add that into a permission list in a RecyclerView.

Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
String miui = (String) get.invoke(c, "ro.miui.ui.version.name");
if (miui != null && miui.contains("11")) {
            PermissionData mPopup = new PermissionData();
            mPopup.text = "Other permissions > Display pop-up while in background";
            mPopup.onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                }
            };

            mPermissionData.add(mPopup);
}

UPDATE: To check if this permission is granted you could use an already running service and launch a dummy transparent activity, and in the onCreate call back on a LocalBroadcastManager or similar and you'll know it's granted. It's an ugly solution, but it may be useful for some.

Lal answered 17/12, 2019 at 10:23 Comment(15)
thank you. that's really the issue. is there any way to request this permission within the app ? or add it programmatically ?Perithecium
None that I'm aware of. I'm redirecting to app info screen and telling them to enable that if they're on MIUI..Lal
but to do that you need to start the app. how do you do it ? through notifications ?Perithecium
I do it when I request another required permission when the app is being first configured.So from the main app, not a service.Lal
@AlexNewman do you have some code that you can show / share about detecting Miui and redirecting to the correct settings page?Disclosure
@alex-newman thanks for your updated answer, but SystemProperties is retrieved from System.getProperties()? if that is the case, i do not have the property you mentioned in your snippet on my redmi note 7 running miui11 (which is android 9 not 10)Disclosure
Edited once again, didn't realise SystemProperties is a hidden Api. Also made it check for Android 9+ as it seems their Android 9 also uses Miui 11 on some devices.Lal
Really useful information for MIUI, as just giving permission started activity from Background service w/o changing any code.Mackinaw
@AlexNewman how can I check that "Display pop-up while in background" is allowed or denied?Roan
@Roan I'm not aware of such a method unfortunately.Lal
For those interested, I have updated the answer with a (hacky) solution to check if the permission is granted.Lal
@AlexNewman Awesome work!. I have been testing my apps on Redmi device for a week trying to figure out why I could not get the activity started. My Redmi is MIUI 11 on Android 8Calcification
I added a better check for MIUI 11 regardless of Android version.Lal
I have a Mi 6X running MIUI11.0.5 (Android 9) and setting this permission, doesn't help. I get the same log message but no Activity.Reticulation
The "start in background" permission also present in MIUI 10, and also it may be in MIUI 12 and so on, so it's probably not a very good thing to check specifically for MIUI 11.Nitramine

© 2022 - 2024 — McMap. All rights reserved.