skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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());
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search