2013-05-09 48 views
0

大家好我是一個新手,學習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()

  1. 下面的代碼把紅線的代碼下方與此錯誤

「在類型MainActivity方法getCalendarUriBase(Activity)不適用於參數(new View.OnClickListener(){})」,代碼:

Uri.parse(getCalendarUriBase(this)+ "reminders"); 
  1. 什麼是下一行的歷元,日期和時間的返回類型?

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根本不是用戶。

    謝謝。

    回答

    2

    聽起來像有兩件事情正在進行:

    1)您有什麼示例代碼是做或手段的一些基本問題。這些都是基本的Java相關的問題

    2)你與日曆API(可能是前API 14)的工作,它的複雜

    關於1關於「在onClickListenerEvent(問題)」:你可以」在調用getCalendarBaseUri()方法時,使用「this」作爲對該活動的引用,因爲此時您處於匿名類中。通過在類名前加上「this」關鍵字來限定它。例如,「MyActivity.this」

    關於第二個問題,「getTime()」將返回一個長整數(參見http://developer.android.com/reference/java/util/Date.html)。

    最後,在活動類「managedQuery()」方法在API 11可能會或可能不算什麼,你已被否決(取決於你的項目的目標)。

    一切的一切,在我看來,日曆代碼是有點複雜得多,它需要的(可能是因爲原作者正試圖以支持推動事件前API 14?)。查看CalendarContract(http://developer.android.com/reference/android/provider/CalendarContract.html),如果您有奢侈的定位API 14+。