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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| package com.example.rappelagenda;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.provider.CalendarContract.Events;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
static Context mContext;
Button buttonCharg;
int dMonth = Calendar.MONTH-1;//month(0 to 11)
int dDay = Calendar.DAY_OF_WEEK_IN_MONTH;
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date dDate = new Date(cal.getTimeInMillis());
int dYear = dDate.getYear() + 1900; //depreciated
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainActivity.mContext = getApplicationContext();
setContentView(R.layout.activity_main);
buttonCharg = (Button)findViewById(R.id.CalendarButton);
buttonCharg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addEvent();
}
});
}
private void addEvent()
{
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 0);// for 0 hour
cal1.set(Calendar.MINUTE, 0);// for 0 min
cal1.set(Calendar.SECOND, 0);// for 0 sec
cal1.set(dYear, 03-1, 1);// for Date [year,month(0 to 11), date]
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, 0);// for 0 hour
cal2.set(Calendar.MINUTE, 0);// for 0 min
cal2.set(Calendar.SECOND, 0);// for 0 sec
cal2.set(dYear, 04-1, 1);// for Date [year,month(0 to 11), date]
Calendar cal3 = Calendar.getInstance();
cal3.set(Calendar.HOUR_OF_DAY, 0);// for 0 hour
cal3.set(Calendar.MINUTE, 0);// for 0 min
cal3.set(Calendar.SECOND, 0);// for 0 sec
cal3.set(dYear, 05-1, 1);// for Date [year,month(0 to 11), date]
Date dDay01 = new Date(cal1.getTimeInMillis());// calendar gives long value
Date dDay02 = new Date(cal2.getTimeInMillis());// calendar gives long value
Date dDay03 = new Date(cal3.getTimeInMillis());// calendar gives long value
Date[] Dates = { dDay01, dDay02, dDay03 };
String txt01 = "01 MARS 2022 : DENTISTE";
String txt02 = "01 AVRIL 2022 : COIFFEUR";
String txt03 = "01 MAI 2022 : MEDECIN";
String[] txt = { txt01, txt02, txt03 };
int i;
int l = Dates.length;
for (i = 0; i < l; i++)
{
long calID = 4;
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, Dates[i].getTime());
values.put(CalendarContract.Events.DTEND, Dates[i].getTime()+60*60);
values.put(CalendarContract.Events.TITLE, txt[i]);
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.LONG);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
ContentUris.appendId(builder, Dates[i].getTime());
Intent intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
startActivity(intent);
}
}
} |
Partager