1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
private void insertNewEvent() {
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2015, 2, 9, 14, 53);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2015, 2, 9, 14, 54);
endMillis = endTime.getTimeInMillis();
String letitre = "Nouveau événement 7";
addToCalendar(this,letitre,startMillis,endMillis);
}
public void addToCalendar(Context ctx, final String title, final long dtstart, final long dtend) {
final ContentResolver cr = ctx.getContentResolver();
Cursor cursor ;
if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
else
cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
if ( cursor.moveToFirst() ) {
final String[] calNames = new String[cursor.getCount()];
final int[] calIds = new int[cursor.getCount()];
for (int i = 0; i < calNames.length; i++) {
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
cursor.moveToNext();
}
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ContentValues cv = new ContentValues();
cv.put("calendar_id", calIds[which]);
cv.put("title", title);
cv.put("description","Application Development ");
cv.put("dtstart", dtstart );
cv.put("hasAlarm", 1);
cv.put("dtend", dtend);
Uri newEvent ;
if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
else
newEvent = cr.insert(Uri.parse("content://calendar/events"), cv);
if (newEvent != null) {
long id = Long.parseLong( newEvent.getLastPathSegment() );
Long identif = id;
Log.d("valeur de identif ", identif.toString());
ContentValues values = new ContentValues();
values.put( "event_id", id );
values.put( "method", 3); // Method SMS
values.put( "minutes", 10 ); // 10 minutes
if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
cr.insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
else
cr.insert( Uri.parse( "content://calendar/reminders" ), values );
}
dialog.cancel();
}
});
builder.create().show();
}
cursor.close();
} |
Partager