How Do We Distinguish Never-Asked From Stop-Asking in Android M's Runtime Permissions?

后端 未结 10 678
萌比男神i
萌比男神i 2020-11-28 20:48

When it comes to the M Developer Preview runtime permissions, according to Google:

  1. If you have never asked for a certain permission before, just ask for it<

10条回答
  •  半阙折子戏
    2020-11-28 21:10

    There is no need to create a parallel persistent state for the permission state, you could just use this method which returns the current permission state at any time:

    @Retention(RetentionPolicy.SOURCE)
        @IntDef({GRANTED, DENIED, BLOCKED})
        public @interface PermissionStatus {}
    
        public static final int GRANTED = 0;
        public static final int DENIED = 1;
        public static final int BLOCKED = 2;
    
        @PermissionStatus 
        public static int getPermissionStatus(Activity activity, String androidPermissionName) {
            if(ContextCompat.checkSelfPermission(activity, androidPermissionName) != PackageManager.PERMISSION_GRANTED) {
                if(!ActivityCompat.shouldShowRequestPermissionRationale(activity, androidPermissionName)){
                    return BLOCKED;
                }
                return DENIED;
            }
            return GRANTED;
        }
    

    Caveat: returns BLOCKED the first app start, before the user accepted/denied the permission through the user prompt (on sdk 23+ devices)

    I also used this answered here.

提交回复
热议问题