I followed the instructions in this page: https://developer.android.com/training/permissions/requesting.html It looks like I didn't miss a thing but still I'm getting the red zigzag saying: 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`
My flow is simple: I call an intent to get a date and when I get one I do the following:
The line:
Cursor cursor = this.getBaseContext().getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null)
gives me the red zigzag I mentioned above, the thing is, I already asked for the permission before calling this function. The only way to get rid of the red zigzag is asking for the permission just above it but it's async and the result is retrieved in a callback... What am I missing?
ps: I'm new here and didn't find how to format the code above to show as a code... please excuse me
My flow is simple: I call an intent to get a date and when I get one I do the following:
Code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
theDate = data.getLongExtra("theDate", theDate);
if (theDate != 0) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, Manifest.permission.READ_CALENDAR)) {
Toast.makeText(getApplicationContext(),"Please",Toast.LENGTH_LONG).show();
}
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.READ_CALENDAR}, 1);
} else
doTheCalendarRead();
} else {
Toast.makeText(getApplicationContext(),"fail",Toast.LENGTH_LONG).show();
}
}
}
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),"Oh well...",Toast.LENGTH_LONG).show();
} else
doTheCalendarRead();
}
default: super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
and the actual permission-required action:
private void doTheCalendarRead() {
String[] projection = new String[]{CalendarContract.Events.TITLE, CalendarContract.Events.EVENT_LOCATION};
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(theDate);
startTime.set(Calendar.HOUR_OF_DAY, 0);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.SECOND, 0);
startTime.set(Calendar.MILLISECOND, 0);
Calendar endTime = Calendar.getInstance();
endTime.setTimeInMillis(theDate);
endTime.set(Calendar.HOUR_OF_DAY, 23);
endTime.set(Calendar.MINUTE, 59);
endTime.set(Calendar.SECOND, 59);
endTime.set(Calendar.MILLISECOND, 999);
String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ))";
Cursor cursor = this.getBaseContext().getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null);
if (cursor.moveToFirst()) {
do {
Toast.makeText( this.getApplicationContext(), "Title: " + cursor.getString(1) + " Start-Time: " + (new Date(cursor.getLong(3))).toString(), Toast.LENGTH_LONG ).show();
} while ( cursor.moveToNext());
}
}
Cursor cursor = this.getBaseContext().getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null)
gives me the red zigzag I mentioned above, the thing is, I already asked for the permission before calling this function. The only way to get rid of the red zigzag is asking for the permission just above it but it's async and the result is retrieved in a callback... What am I missing?
ps: I'm new here and didn't find how to format the code above to show as a code... please excuse me
Last edited: