Skip to content

Instantly share code, notes, and snippets.

@chartsai
Last active June 8, 2016 16:56
Show Gist options
  • Save chartsai/c06f98b4e5158e720726a47a3fa476d2 to your computer and use it in GitHub Desktop.
Save chartsai/c06f98b4e5158e720726a47a3fa476d2 to your computer and use it in GitHub Desktop.
package idv.chatea.calendar;
import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
// Projection array. Creating indices for this array instead of doing
// dynamic lookups improves performance.
public static final String[] EVENT_PROJECTION = new String[] {
Calendars._ID, // 0
Calendars.ACCOUNT_NAME, // 1
Calendars.CALENDAR_DISPLAY_NAME, // 2
Calendars.OWNER_ACCOUNT, // 3
Events.DESCRIPTION, // 4
};
// The indices for the projection array above.
private static final int PROJECTION_ID_INDEX = 0;
private static final int PROJECTION_ACCOUNT_NAME_INDEX = 1;
private static final int PROJECTION_DISPLAY_NAME_INDEX = 2;
private static final int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
private static final int PROJECTION_DESCRIPTION_INDEX = 4;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
runQuery();
}
});
askPermission();
}
@Override
protected void onStart() {
super.onStart();
}
private void askPermission() {
// Submit the query and get a Cursor object back.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_CALENDAR}, 100);
return;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 100) {
boolean allGrant = true;
for (int result: grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
allGrant = false;
}
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@SuppressWarnings("MissingPermission")
private void runQuery() {
// Run query
Cursor cur = null;
ContentResolver cr = getContentResolver();
Uri uri = Events.CONTENT_URI;
String selection = "";
String[] selectionArgs = new String[]{};
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
while (cur.moveToNext()) {
long calID = 0;
String displayName = null;
String accountName = null;
String ownerName = null;
String description = null;
// Get the field values
calID = cur.getLong(PROJECTION_ID_INDEX);
displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
description = cur.getString(PROJECTION_DESCRIPTION_INDEX);
// Do something with the values...
Log.d("TAG", calID + "," + displayName + "," + description);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment