Android日历检查权限 [英] Android Calendar Check Permission

查看:1511
本文介绍了Android日历检查权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实现日历提供程序,但是我偶然发现了一个我想防止的小错误.

I'm currently implementing the calendar provider however i've stumbled upon a minor error that i'd like to prevent.

ContentResolver contentResolver = getContentResolver();
ContentValues contentValues = new ContentValues();

....

Uri uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, contentValues); // Error pops here

呼叫需要许可,用户可能会拒绝它:代码应显式检查以查看许可是否可用(使用checkPermission)或显式处理潜在的"SecurityException"

"Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential `SecurityException"

防止此错误的最佳方法是什么?

What is the best method to prevent this error?

任何帮助将不胜感激.

推荐答案

首先处理android权限,检查它们是否可用,如果没有,您可以如下所示进行请求

Handle android permission first , check if they are available , if not you can request them as shown below

仅在拥有权限的情况下使用您的功能

proceed with your functionality only if permissions are available

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED 
    && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);}else if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED 
        && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED){

ContentResolver contentResolver = getContentResolver();
ContentValues contentValues = new ContentValues();
Uri uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, contentValues);}

这篇关于Android日历检查权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆