请求写入外部存储许可

时间:2018-04-16 18:29:15

标签: java android

我有一个按钮,当按下时,请求一次,但如果它说不,它不会再问我

如果他说不,他怎么能表明他可以再问一次?

检查权限

 <audio controls autoplay="false" preload="none">
      <source src="http://a.tumblr.com/tumblr_lrdjzp8jEq1r2hak2o1.mp3" type="audio/ogg">
    </audio>

我的代码权限 - Main.java

            if (Build.VERSION.SDK_INT >= 23){

            if (checkPermission()){
                // Code for above or equal 23 API Oriented Device
                // Your Permission granted already .Do next code
            } else {
                requestPermission(); // Code for permission
            }
        }
        else{
            // Code for Below 23 API Oriented Device
            // Do next code
        }

2 个答案:

答案 0 :(得分:3)

试试这个: Myclass用于运行时权限:

public class RequestPermissionHandler {
    private Activity mActivity;
    private RequestPermissionListener mRequestPermissionListener;
    private int mRequestCode;

public void requestPermission(Activity activity, @NonNull String[] permissions, int requestCode,
        RequestPermissionListener listener) {
    mActivity = activity;
    mRequestCode = requestCode;
    mRequestPermissionListener = listener;

    if (!needRequestRuntimePermissions()) {
        mRequestPermissionListener.onSuccess();
        return;
    }
    requestUnGrantedPermissions(permissions, requestCode);
}

private boolean needRequestRuntimePermissions() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

private void requestUnGrantedPermissions(String[] permissions, int requestCode) {
    String[] unGrantedPermissions = findUnGrantedPermissions(permissions);
    if (unGrantedPermissions.length == 0) {
        mRequestPermissionListener.onSuccess();
        return;
    }
    ActivityCompat.requestPermissions(mActivity, unGrantedPermissions, requestCode);
}

private boolean isPermissionGranted(String permission) {
    return ActivityCompat.checkSelfPermission(mActivity, permission)
            == PackageManager.PERMISSION_GRANTED;
}

private String[] findUnGrantedPermissions(String[] permissions) {
    List<String> unGrantedPermissionList = new ArrayList<>();
    for (String permission : permissions) {
        if (!isPermissionGranted(permission)) {
            unGrantedPermissionList.add(permission);
        }
    }
    return unGrantedPermissionList.toArray(new String[0]);
}

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
    if (requestCode == mRequestCode) {
        if (grantResults.length > 0) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    mRequestPermissionListener.onFailed();
                    return;
                }
            }
            mRequestPermissionListener.onSuccess();
        } else {
            mRequestPermissionListener.onFailed();
        }
    }
}

public interface RequestPermissionListener {
    void onSuccess();

    void onFailed();
}

}

Activity中使用,如下所示:

public class MainActivity extends AppCompatActivity {
    private RequestPermissionHandler mRequestPermissionHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRequestPermissionHandler = new RequestPermissionHandler();

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleButtonClicked();
            }
        });
    }

    private void handleButtonClicked(){
        mRequestPermissionHandler.requestPermission(this, new String[] {
                Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_EXTERNAL_STORAGE
        }, 123, new RequestPermissionHandler.RequestPermissionListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(MainActivity.this, "request permission success", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed() {
                Toast.makeText(MainActivity.this, "request permission failed", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
            @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mRequestPermissionHandler.onRequestPermissionsResult(requestCode, permissions,
                grantResults);
    }
}

代码行为的Gif图片链接。

https://i.stack.imgur.com/ha5LT.gif

答案 1 :(得分:1)

根据ActivityCompat.requestPermissions()的结果,不要将您的电话定位到ActivityCompat.shouldShowRequestPermissionRationale()shouldShowRequestPermissionRationale()方法用于确定是否在屏幕上显示某些内容以证明您需要权限的原因(如果用户拒绝了请求),而不是您是否应该请求它。如果它返回false,则系统会要求用户再次询问,并且您不应该尝试证明该权限。

您可能会发现此演讲很有帮助:https://youtu.be/WGz-alwVh8A