大家好我是一個新手,學習Android的...我得到了來自this post寫了下面的代碼通過@AbhiAndroid的日曆事件發出
Put reminder in real calendar on the phone?
這個帖子並提供答案,唯一的代碼。我想有人幫助我,我有這個問題,首先代碼:
代碼onClickListenerEvent低於:
btnOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Calendar dateToShow = Calendar.getInstance();
// dateToShow.set(2013, Calendar.MAY, 10, 9, 0);
//
// showCalendarAtTime(dateToShow);
Uri event1;
long epoch, epoch1;
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
try
{
epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
//epoch=epoch;
Log.e("epoch",String.valueOf(epoch));
epoch1 = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
//epoch1=epoch1;
Log.e("epoch1",String.valueOf(epoch1));
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
values.put("calendar_id", 1);
values.put("title", "Appoitment");
values.put("allDay", 0);
values.put("dtstart",epoch); // event starts at 11 minutes from now
values.put("dtend", epoch1); // ends 60 minutes from now
values.put("description", "Your consulting date and time ");
values.put("visibility", 0);
values.put("hasAlarm", 1);
if(EVENTS_URI!=null){
event1 = cr.insert(EVENTS_URI, values);
}
// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put("event_id", Long.parseLong(event1.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 10);
if(REMINDERS_URI!=null){
cr.insert(REMINDERS_URI, values);
}
alertDialog.setTitle("Event Saved");
Dismiss();
alertDialog.show();
}
// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this)+ "reminders");
values = new ContentValues();
values.put("event_id", id);
values.put("method", 1);
values.put("minutes", 0);
cr.insert(REMINDERS_URI, values);
}
});
getCalendarUriBase代碼:
private String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
}
我有問題與上面的代碼,我發現在帖子中的答案很少,如event1
是一個URI類型變量,但我有以下問題:
onC中的問題lickListenerEvent()
- 下面的代碼把紅線的代碼下方與此錯誤
「在類型MainActivity
方法getCalendarUriBase(Activity)
不適用於參數(new View.OnClickListener(){})
」,代碼:
Uri.parse(getCalendarUriBase(this)+ "reminders");
- 什麼是下一行的歷元,日期和時間的返回類型?
epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
什麼是下面的代碼?因爲在eclipse中它說
alertDialog
,有8個修復程序可用:第一個是「創建新變量」,如果是的話,什麼類型的變量,什麼是解僱?我是否需要他們獲得結果或在日曆中添加事件?alertDialog.setTitle("Event Saved");
Dismiss();
alertDialog.show();
在Eclipse中以下行我得到一個過時的警告:
managedCursor = act.managedQuery(calendars, null, null, null, null);
@Abhi或任何有經驗的人都可以幫助我嗎?由於我是新手,並且Android根本不是用戶。
謝謝。