2013-02-27 173 views
1

在我的應用程序中,我想使用代碼/編程方式打開設備默認日曆。我使用此代碼:無法查看日曆

private void viewAllCalender() { 
     // TODO Auto-generated method stub 
     Intent i = new Intent(); 
     if(Build.VERSION.SDK_INT >= 8 && Build.VERSION.SDK_INT <= 14){ 
      i.setClassName("com.android.calendar","com.android.calendar.LaunchActivity"); 
     }else if(Build.VERSION.SDK_INT >= 15){  
      i.setClassName("com.google.android.calendar", "com.android.calendar.LaunchActivity"); 
     }else{ 
      i.setClassName("com.android.calendar","com.android.calendar.LaunchActivity"); 
     } 
     startActivity(i); 
    } 

,它正在爲所有的設備,但它不工作三星S3 - (BUILD SDK版本 - 17)

請幫我找出什麼問題是?

謝謝

回答

2

你必須認識到,你不能指望Android設備有一個特定的應用程序。甚至連遊戲應用程序都不能安裝。這樣做的正確方法是不使用.setClassName,然後讓用戶決定要做什麼。

有十幾個不同的日曆應用程序和手機制造商都有自己的...

編輯

如果你想添加一個事件到日曆,你可以用我的CalendarOrganizer它處理一個很多這些問題:

public class CalendarOrganizer { 
    private final static int ICE_CREAM_BUILD_ID = 14; 
    /** 
    * Creates a calendar intent going from startTime to endTime 
    * @param startTime 
    * @param endTime 
    * @param context 
    * @return true if the intent can be handled and was started, 
    * false if the intent can't be handled 
    */ 
    public static boolean createEvent(long startTime, long endTime, String title, String description, 
      String location, boolean isAllDay, Context context) { 
     Intent intent = new Intent(Intent.ACTION_EDIT); 
     int sdk = android.os.Build.VERSION.SDK_INT; 
     if(sdk < ICE_CREAM_BUILD_ID) { 
      // all SDK below ice cream sandwich 
      intent.setType("vnd.android.cursor.item/event"); 
      intent.putExtra("beginTime", startTime); 
      intent.putExtra("endTime", endTime); 
      intent.putExtra("title", title); 
      intent.putExtra("description", description); 
      intent.putExtra("eventLocation", location); 
      intent.putExtra("allDay", isAllDay); 

//   intent.putExtra("rrule", "FREQ=YEARLY"); 
     } else { 
      // ice cream sandwich and above 
      intent.setType("vnd.android.cursor.item/event"); 
      intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime); 
      intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime); 
      intent.putExtra(Events.TITLE, title); 
      intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE); 
      intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , isAllDay); 
      intent.putExtra(Events.DESCRIPTION, description); 
      intent.putExtra(Events.EVENT_LOCATION, location); 

//   intent.putExtra(Events.RRULE, "FREQ=DAILY;COUNT=10") 
     } 
     try { 
      context.startActivity(intent); 
      return true; 
     } catch(Exception e) { 
      return false; 
     } 
    } 
} 
+0

感謝您的回覆。您能否詳細說明我如何在三星S3中解決這個問題? – 2013-02-27 06:42:17

+0

@BorntoWin我已經添加了一個演示如何將事件添加到日曆的示例,但是如果這是您要查找的內容,則不清楚您的帖子。如果你想打開日曆應用程序,那麼你沒有運氣。你可以像我一樣做,並在其周圍放置一個try catch塊,在應用程序不在電話中時通知用戶... – Warpzit 2013-02-27 08:58:51