2013-02-27 6 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); 
    } 

ЭТО РАБОТА ДЛЯ ВСЕХ УСТРОЙСТВ, НО ЭТО НЕ РАБОТА В SAMSUNG 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

Спасибо за ответ. Можете ли вы рассказать, как я могу решить эту проблему в Samsung S3? –

+0

@BorntoWin Я добавил пример, показывающий, как добавить событие в календарь, но это не ясно из вашего сообщения, если это то, что вы ищете. Если вы хотите открыть приложение календаря, вам не повезло. Вы можете сделать так, как я, и поместить блок catch try вокруг него, чтобы уведомить пользователя, когда приложение не на телефоне ... – Warpzit

Смежные вопросы