My code looks something like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), DateSelectorActivity.class);
startActivityForResult(i, 1);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
long theDate = 0;
theDate = data.getLongExtra("theDate", theDate);
if (theDate != 0) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_CALENDAR)) {
Toast.makeText(getApplicationContext(),"Please?!",Toast.LENGTH_LONG).show();
}
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CALENDAR}, 1);
return;
}
doTheCalendarReading();
} 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 request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
doTheCalendarReading();
} else {
Toast.makeText(getApplicationContext(),"Oh well...",Toast.LENGTH_LONG).show();
}
}
}
}
private void doTheCalendarReading() {
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());
}
}
The flow in general is: When pressing a button, I open an activity to receive a date (that's working fine). Once I get the date, I want to retrieve the calendar events on that date.
Regarding the line
Cursor cursor = this.getBaseContext().getContentResolver()
.query(CalendarContract.Events.CONTENT_URI,
projection, selection, null, null);
I got a red zigzag telling me I should ask for the relevant permission.
So I did and if the permission already granted I call doTheCalendarReading() and if not, I ask for the permission and if granted, again I call doTheCalendarReading().
I moved the code that requires the permission into that function but again it gives me a red zigzag telling me I need to ask for permission.
Now I'm confused.
Aucun commentaire:
Enregistrer un commentaire