24

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 ?

ilker Aktuna
  • 335
  • 3
  • 8
  • 1
    Same problem on MIUI 11. – AlexS Dec 17 '19 at 05:19
  • a fix can found in https://stackoverflow.com/questions/59418504/xiaomi-devices-permission-to-enable-apps-pop-up-windows-while-running-in-the-bac/65172936#65172936 has todo with Google play permissions and target SDKs – BennyP Dec 07 '20 at 11:03

1 Answers1

31

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.

Alex Newman
  • 1,369
  • 12
  • 34
  • thank you. that's really the issue. is there any way to request this permission within the app ? or add it programmatically ? – ilker Aktuna Dec 18 '19 at 16:31
  • None that I'm aware of. I'm redirecting to app info screen and telling them to enable that if they're on MIUI.. – Alex Newman Dec 18 '19 at 16:38
  • but to do that you need to start the app. how do you do it ? through notifications ? – ilker Aktuna Dec 19 '19 at 17:18
  • I do it when I request another required permission when the app is being first configured.So from the main app, not a service. – Alex Newman Dec 20 '19 at 09:01
  • @AlexNewman do you have some code that you can show / share about detecting Miui and redirecting to the correct settings page? – Alienpenguin Jan 13 '20 at 22:22
  • @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) – Alienpenguin Jan 14 '20 at 10:31
  • 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. – Alex Newman Jan 14 '20 at 13:06
  • 2
    Really useful information for MIUI, as just giving permission started activity from Background service w/o changing any code. – Panache Jan 19 '20 at 12:11
  • @AlexNewman how can I check that "Display pop-up while in background" is allowed or denied? – Cheerag Jan 23 '20 at 13:26
  • @Cheerag I'm not aware of such a method unfortunately. – Alex Newman Jan 24 '20 at 08:47
  • For those interested, I have updated the answer with a (hacky) solution to check if the permission is granted. – Alex Newman Mar 03 '20 at 20:18
  • @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 8 – Yazid Mar 27 '20 at 04:43
  • I added a better check for MIUI 11 regardless of Android version. – Alex Newman Mar 27 '20 at 12:35
  • 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. – Mark May 19 '20 at 03:41
  • 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. – Alexey Timokhin May 19 '21 at 07:22