9

This question is already asked twice but has no answer Apps installed on Xiaomi devices need a permission called 'show on lock screen' in order to allow activity to start if the device is locked How can I prompt the user to enable this permission programmaticaly in android studio, Some messaging apps like Messenger,Telegram,Whatapp have this permission already enabled I already added these four flags but the activity is not shown if the permission is not granted

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

Thanks in advance

Ahmed Rajab
  • 603
  • 3
  • 10
  • 28
  • Hello Ahmed, I'm facing the same problem. How did you solve it? – Arjun May 05 '21 at 07:46
  • 1
    @Arjun I didn't, I check whether the device is a Xiaomi and show a message with instructions of how to move manually to the proper settings and allow it – Ahmed Rajab May 05 '21 at 10:16

2 Answers2

0

Here is the doc that shows How to create a widgets: https://developer.android.com/guide/topics/appwidgets/index.html#lockscreen

you have to create a widget that have a flags that sets to

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

that help the user to interact on lockscreen

AndroWaqar
  • 231
  • 2
  • 9
0

i didn't find the proper way to request permission but if you want to move manually to the proper settings and allow it, here's the way. i hope it's work for you.

public static void goToNotificationSettings(Context context) {
    Intent intent = new Intent();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts(SCHEME, context.getPackageName(), null));
    } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
        intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
        intent.putExtra("app_package", context.getPackageName());
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
        intent.putExtra("app_package", context.getPackageName());
        intent.putExtra("app_uid", context.getApplicationInfo().uid);
    } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setData(Uri.parse("package:" + context.getPackageName()));
    } else {
        return;
    }
    context.startActivity(intent);
}

EDIT:

for check Permission you can use this line of code and then call the function.

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_NOTIFICATION_POLICY) != PackageManager.PERMISSION_GRANTED) {
        goToNotificationSettings(getActivity());
    }