其他分享
首页 > 其他分享> > 2021-02-27

2021-02-27

作者:互联网

public class MainActivity extends AppCompatActivity {
private static final String TAG = “MainActivity”;
private static final int PERMISSION_REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //版本检查
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkCalendarPermission();
    }
}

private void checkCalendarPermission(){
    int readPermission = checkSelfPermission(Manifest.permission.READ_CALENDAR);
    int writePermission = checkSelfPermission(Manifest.permission.READ_CALENDAR);
    if(readPermission == PackageManager.PERMISSION_GRANTED && writePermission == PackageManager.PERMISSION_GRANTED){
        //表示有权限
    } else {
        Log.d(TAG, "checkCalendarPermission。。。 ");
      //去获取权限
        //做个提示,用户点击了确定再去调用权限,如果点击了不在提示,就不再获取了
      requestPermissions(new String[]{Manifest.permission.READ_CALENDAR,Manifest.permission.READ_CALENDAR},PERMISSION_REQUEST_CODE);

    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == PERMISSION_REQUEST_CODE){
        //判断结果
        if (grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "onRequestPermissionsResult: has permission");
            //有权限
        } else{
            //没有权限
            Log.d(TAG, "onRequestPermissionsResult: no permission");
            finish();
        }
    }
}

}

标签:02,27,permission,int,GRANTED,PERMISSION,READ,TAG,2021
来源: https://blog.csdn.net/qq_42021348/article/details/114187010