如何解决需要的权限CALENDAR_READ或CALENDAR_WRITE?

时间:2019-05-08 05:33:10

标签: java android

代码显示

  

原因:java.lang.SecurityException:权限被拒绝:正在打开   提供者com.android.providers.calendar.CalendarProvider2来自   ProcessRecord {ad38a2a 30100:com.example.myapplication / u0a231}   (pid = 30100,uid = 10231)需要android.permission.READ_CALENDAR或   android.permission.WRITE_CALENDAR

这是时间轴日历视图,可通过缩放以线性视图显示日历

manifest.xml

<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>

MainActivity.java

    calstuff.LoadCalendars();
    Log.d(LogTag, "calendars loaded: " + calstuff.ourCalendars.size());

    calstuff.LoadEvents();
    Log.d(LogTag, "events loaded: " + calstuff.ourEvents.size());

CalStuff.java

     Cursor calCursor = context.getContentResolver().query(uri, 
     projection, Calendars.VISIBLE + " = 1",
            null, Calendars._ID + " ASC");

我希望显示线性日历时间轴,日历可以按年,月,周,日缩放,但显示错误。

1 个答案:

答案 0 :(得分:0)

您需要运行时权限,如下所示:

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 Calendar Check Permission

有关更多信息,请参见下面的链接:

https://developer.android.com/training/permissions/requesting