2012-03-05 55 views
5

我使用未公開的API來爲Android OS 2.2添加日曆事件。現在Android OS 4已經不在了,我使用的API自然不起作用!我在14級看了Android SDK的日曆api,但是我無法找到如何在我使用Android OS 2.2創建的項目中使用它的線索。因爲當我在我的項目中使用CalendarContract類時,它顯示錯誤。所以我無法找到我在使用Android OS 2.2創建的項目中要做什麼以及如何使用這個類的線索。在Android操作系統中的Android日曆集成4

如果有人有任何樣品可供分享或樣品鏈接,請告訴我。

日曆API類

public abstract class CalendarAPI 
{ 
private static CalendarAPI api; 

public static CalendarAPI getAPI() 
{ 
    String apiClass; 
    if (Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.FROYO) { 
     apiClass = "com.example.calendar.CycleManagerSDK8 "; 
    } else { 
     apiClass = "com.example.calendar.CycleManagerSDK14 "; 
    } 

    try 
    { 
     Class<? extends CalendarAPI> realClass = Class.forName(apiClass).asSubclass(CalendarAPI.class); 
     api = realClass.newInstance(); 
    } 
    catch (Exception e) 
    { 
     throw new IllegalStateException(e); 
    } 

    return api; 
} 

public abstract boolean setAlertOnDevice(Context c) ; 
} 

類SDKVersion 8 - 13

public class CycleManagerSDK8 extends CalendarAPI 
{ 
public boolean setAlertOnDevice(Context c) 
{     
    Resources res = c.getResources(); 

    Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events"); 

    Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/" + "reminders"); 

     ContentResolver cr = c.getContentResolver(); 

    Date dtStartDate = getStartDate(); 

    Calendar cal = Calendar.getInstance(); 

    cal.setTime(dtStartDate); 
    cal.add(Calendar.DATE, m_iStart); 

    cal.set(Calendar.HOUR_OF_DAY, 8); 
    cal.set(Calendar.MINUTE, DEFAULT_TIME_OF_DATE);  
    cal.set(Calendar.SECOND, DEFAULT_TIME_OF_DATE); 
    cal.set(Calendar.MILLISECOND, DEFAULT_TIME_OF_DATE); 

    String str = m_reminderText + res.getString(R.string.alert_start);           
    m_strDescription = res.getString(R.string.alert_start_msg); 

    ContentValues values = new ContentValues(); 
    values.put("calendar_id", 1); 
    values.put("title", str); 
    values.put("description", m_strDescription);      
    values.put("dtstart", cal.getTimeInMillis()); 
    values.put("dtend", cal.getTimeInMillis()); 
    values.put("hasAlarm", 1); 
    Uri event = cr.insert(EVENTS_URI, values); 

    m_calendarEvents[m_calendarEventCount] = event; 
    m_calendarEventCount = m_calendarEventCount + 1; 

    values = new ContentValues(); 
    values.put("event_id", Long.parseLong(event.getLastPathSegment())); 
    values.put("method", 1); 
    values.put("minutes", 10); 
    cr.insert(REMINDERS_URI, values); 
} 

}

類SDKVersion 14

public class CycleManagerSDK14 extends CalendarAPI 
{ 
     public void setAlertOnDevice(Context c) 
    { 
     long startMillis = 0; 
     long endMillis = 0;  
     Calendar beginTime = Calendar.getInstance(); 
     beginTime.set(2012, 2, 2, 7, 0); 
     startMillis = beginTime.getTimeInMillis(); 
     Calendar endTime = Calendar.getInstance(); 
     endTime.set(2012, 2, 2, 7, 0); 
     endMillis = endTime.getTimeInMillis(); 

     ContentResolver cr = getContentResolver(); 
     ContentValues values = new ContentValues(); 

     values.put(CalendarContract.Events.DTSTART, startMillis); 
     values.put(CalendarContract.Events.DTEND, endMillis); 
     values.put(CalendarContract.Events.TITLE, "Walk The Dog"); 
     values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!"); 
     values.put(CalendarContract.Events.CALENDAR_ID, 1); 
     values.put(CalendarContract.Events.EVENT_TIMEZONE, "eventTimezone"); 
      cr.insert(CalendarContract.Events.CONTENT_URI, values); 
    } 

}

謝謝。

+1

pl。發佈你的錯誤或異常 – Vinay 2012-03-05 07:09:45

+0

在CalendarContract類沒有發現異常 – Rahul 2012-03-05 07:19:01

回答

3

據我瞭解,你有2個問題...

  1. 您需要訪問日曆2.2(API 8及以上)
  2. 從API 14,你需要使用CalendarContract訪問日曆

我的建議是創建一個抽象類並定義個別實現。

例如

String apiClass; 
if (Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.DONUT) { 
    apiClass = "com.example.calendar.version8plus"; 
} else { 
    apiClass = "com.example.calendar.version14plus"; 
} 

try { 
    Class<? extends CalendarAPI> realClass = Class.forName(apiClass).asSubclass(CalendarAPI.class); 
    api = realClass.newInstance(); 
} catch (Exception e) { 
    throw new IllegalStateException(e); 
} 

有了這個,你在編譯時根據您的API級別創建兩個不同的實現按API的...

從編譯排除其他文件,爲此,你需要在Manifest中更改API級別。

+0

現在在我的清單文件中,我已經使用這個所以現在我改變爲什麼 – Rahul 2012-03-05 08:24:27

+0

嗨vinay我發佈我的代碼我已經在我的項目中使用了使用api level 8構建的項目。你能告訴我你的代碼如何在我的項目中使用。 – Rahul 2012-03-05 08:34:05

+0

@RahulBorah,試着在Manifest中將你的API級別提高到14,看看你是否得到了Class not found錯誤? – Vinay 2012-03-05 08:45:10