代码之家  ›  专栏  ›  技术社区  ›  Payam Asefi

记住android棉花糖上的requestPermissions

  •  -3
  • Payam Asefi  · 技术社区  · 10 年前

    我能够在我的活动中使用这行代码处理android 6中的危险权限:

        ActivityCompat.requestPermissions(WriteThreadActivity.this,
                new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE,
                        android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, RESULT_LOAD_IMAGE);
    

    它工作得很好,但是否有任何方法可以防止每次用户打开此活动时显示此弹出窗口?比如记住方法?

    2 回复  |  直到 10 年前
        1
  •  1
  •   John    10 年前
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {
    
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
    
        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    
    } else {
    
        // No explanation needed, we can request the permission.
    
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
    
        // MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
    }else{
      // You have permission granted already, to the task
    }
    
        2
  •  0
  •   Nirav Shah    10 年前
    private boolean doesUserHavePermission()
    {
        int result = context.checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }
    
    推荐文章