Android Calendar Check Permission
I'm currently implementing the calendar provider however i've stumbled upon a minor error that i'd like to prevent. ContentResolver contentResolver = getContentResolver(); ContentV
Solution 1:
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,newString[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);}elseif(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED){
ContentResolvercontentResolver= getContentResolver();
ContentValuescontentValues=newContentValues();
Uriuri= contentResolver.insert(CalendarContract.Events.CONTENT_URI, contentValues);}
Solution 2:
You can always add throws SecurityException to the method and take care of the caught exception further upstream. Furthermore, I would recommend using this library https://github.com/tbruyelle/RxPermissions if you interested in using reactivex methodology to deal with permissioning.
Solution 3:
Your targetSdkVersion is 23 or higher, just reduce it
Post a Comment for "Android Calendar Check Permission"