26

I am trying to open an activity from a class that extends Service. I am performing this task when the app is not in foreground/not being used. I can see in the logs that my service class triggered start activity with Intent.FLAG_ACTIVITY_NEW_TASK flag. But the activity does not open. But when the service triggers the same activity when the app is in foreground/being used, the activity opens.

After few searches I found out that I need to manually give the permission "display popup windows while running in the background android" in the other permissions section in the app settings.

The display over other apps permission SYSTEM_ALERT_WINDOW only gives access to "Display popup window".

Other permissions

In the image above, SYSTEM_ALERT_WINDOW will only grant you access to "Display popup window" and this works fine if the app is in foreground/being used but the permission marked in red if granted will give you access to open any activity directly from a class extending Service.

How do I check or ask the user to give "Display popup windows while running in the background" permission? or Is this permission restricted?

I see apps like Whatsapp have this checked by default.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Sidharth MA
  • 241
  • 1
  • 3
  • 9
  • 1
    https://stackoverflow.com/a/40355440/7557205 Check this answer – Aswin P Ashok Jan 13 '20 at 09:00
  • 1
    Does this answer your question? [How to programmatically grant the "draw over other apps" permission in android?](https://stackoverflow.com/questions/40355344/how-to-programmatically-grant-the-draw-over-other-apps-permission-in-android) – Aswin P Ashok Jan 13 '20 at 09:03
  • Sorry @AswinPAshok, I am using the above solution already, but that is not what I am looking for. Any how thank you so much for the reply. :) I have edited the question with an image for better clarity. – Sidharth MA Jan 14 '20 at 08:08
  • @SidharthMA Did you got any solution for it? – Shayan Pourvatan Feb 02 '20 at 09:37
  • Hi @ShayanPourvatan, Sorry to say this, I am still finding a solution!. Do you have any clue? – Sidharth MA Feb 03 '20 at 10:10
  • 1
    @SidharthMA No, as I've searched we cannot to change permission in runtime, only solution is show headsUp notification to user and ask him to enable it, but we need another permission to show headsUp notification for those user that we can't permit it unfortunately ( show floating notification ) – Shayan Pourvatan Feb 03 '20 at 10:20
  • Does Android 10 fix this, removing this weird setting? I noticed that now it's on the docs, that some cases will be excluded for Android 10 : https://developer.android.com/guide/components/activities/background-starts#exceptions , but I think it's still turned off by default. – android developer Mar 03 '20 at 09:59
  • @SidharthMA interesting question. Is there any solution? I've seen that Facebook auto on this permission. – Bhavin Chauhan May 12 '20 at 15:58
  • @BhavinChauhan, unfortunately no. But I did check the Facebook settings page and found that this permission was turned ON by default. Dont know how!! – Sidharth MA Sep 08 '20 at 07:36
  • 3
    @SidharthMA also I know its default on for white listed all like Uber, Facebook etc – Bhavin Chauhan Sep 08 '20 at 13:06

4 Answers4

8

This is common problem in Xiaomi devices for developers,

the solution we have for the moment is to redirect users to other permissions screen and let the users give the permission manually :

if ("xiaomi" == Build.MANUFACTURER.toLowerCase(Locale.ROOT)) {
    val intent = Intent("miui.intent.action.APP_PERM_EDITOR")
    intent.setClassName("com.miui.securitycenter",
            "com.miui.permcenter.permissions.PermissionsEditorActivity")
    intent.putExtra("extra_pkgname", getPackageName())
    startActivity(intent)
}

You can use Settings.canDrawOverlays(this) to test only if the user has given the Display pop-up windows which means the app can draw over the system ui.

The other two permissions can not be verified if the user has given them or not :

  1. Display pop-up windows while running in the background : this one means to allow the app to draw over the system ui while the app is running in the background like services and broadcast
  2. Show on lock screen : this one gives the app the permission to run even when the screen is locked like social apps (whatsApp , Viber , Messnger ...)
elhoucine ayoub
  • 949
  • 2
  • 12
  • 19
6

Since there is no official way to check the "Display pop-up windows while running in the background" permission, I used a little trick to check the permissions.

Check if the app can draw overlays like below and then if the device is xiaomi navigate the user to the specific settings screen like below.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                if ("xiaomi".equals(Build.MANUFACTURER.toLowerCase(Locale.ROOT))) {
                    final Intent intent =new Intent("miui.intent.action.APP_PERM_EDITOR");
                    intent.setClassName("com.miui.securitycenter",
                            "com.miui.permcenter.permissions.PermissionsEditorActivity");
                    intent.putExtra("extra_pkgname", getPackageName());
                    new AlertDialog.Builder(this)
                            .setTitle("Please Enable the additional permissions")
                            .setMessage("You will not receive notifications while the app is in background if you disable these permissions")
                            .setPositiveButton("Go to Settings", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    startActivity(intent);
                                }
                            })
                            .setIcon(android.R.drawable.ic_dialog_info)
                            .setCancelable(false)
                            .show();
                }else {
                    Intent overlaySettings = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                    startActivityForResult(overlaySettings, OVERLAY_REQUEST_CODE);
                }
            }
        }
Kasun Thilina
  • 1,523
  • 2
  • 14
  • 20
  • `Settings.canDrawOverlays(this)` Will this assure `Display pop-up windows` is given ? – Arul Nov 28 '22 at 17:09
0

Try the following code.

 Intent rIntent = context.getPackageManager()
                    .getLaunchIntentForPackage(context.getPackageName() );
            PendingIntent intent = PendingIntent.getActivity(
                    context, 0,
                    rIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            manager.set(AlarmManager.RTC, System.currentTimeMillis(), intent);
            System.exit(2);
littlebear333
  • 710
  • 2
  • 6
  • 14
  • Hi, the solutions works in a way.. but i would prefer with a permission. Thank you :) – Sidharth MA Jan 15 '20 at 12:28
  • Why the need to call "System.exit(2);" , and why 2 as a parameter? – android developer Mar 03 '20 at 10:12
  • System.exit() terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination. So exit(0) generally used to indicate successful termination. And other parameters such as 1,2,-1 all indicates unsuccessful termination. There is no difference. – littlebear333 Mar 04 '20 at 00:58
  • 2
    How to check that permission is already granted or not? – Muhammad chhota Aug 31 '20 at 04:29
0

I manage to work around my project by following this gist https://gist.github.com/0awawa0/65bf88e43159750f596da194ed923522

@ReactMethod()
    public void isMiuiCanDisplayOverlay(Promise ps){
        boolean isXiaomi = XiaomiUtilities.isMIUI();
        boolean isGranted = XiaomiUtilities.isPermissionGranted(reactContext,XiaomiUtilities.OP_BACKGROUND_START_ACTIVITY);
        if(isXiaomi){
            ps.resolve(isGranted);
        }else{
            ps.resolve(true);
        }
    }
    @ReactMethod()
    public void openMiuiDisplayOverlayPermission(Promise ps){
        boolean isXiaomi = XiaomiUtilities.isMIUI();
        try{
            if(isXiaomi){
                XiaomiUtilities.goToMiuiPermissionActivity_V8(reactContext);
                ps.resolve(true);
            }else{
                ps.resolve(false);
            }
        }catch (Exception e){
            String version = XiaomiUtilities.getMiuiVersion();
            Log.d(LOG_TAG,"Cannot open Miui Display Pop-up window while running in background" + version + e.toString());
        }
    }

    

code above I created in react native to bridge to javascript
asdf
  • 26
  • 1