2013-10-16 95 views
0

我已成功將事件添加到本機日曆中,並且對於Android API級別14或更高版本可以正常工作。ActivityNotFoundException在運行2.3的設備上進行測試時

我試了@ VishalKhakhkhar的代碼,但我仍然無法解決這個問題?我收到提示:未找到可處理意向{行動= android.intent.action.EDIT典型值= vnd.android.cursor.item活動/事件運行2.3.6

看到設備上測試時我下面更新的代碼,嘗試解決此問題: -

Button btnEvents = (Button) findViewById(R.id.button1); 
    btnEvents.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

      long startMillis = 0; 
      long endMillis = 0; 
      Calendar beginTime = Calendar.getInstance(); 
      beginTime.set(2014, 1, 16, 16, 0); 
      startMillis = beginTime.getTimeInMillis(); 
      Calendar endTime = Calendar.getInstance(); 
      endTime.set(2014, 1, 16, 22, 30); 
      endMillis= endTime.getTimeInMillis(); 

      if (Build.VERSION.SDK_INT >= 14) { 
       Intent intent = new Intent(Intent.ACTION_EDIT).setData(Events.CONTENT_URI) 
        .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startMillis) 
        .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endMillis) 
        .putExtra(Events.TITLE, "Title") 
        .putExtra(Events.DESCRIPTION, "Description") 
        .putExtra(Events.EVENT_LOCATION, "Location") 
        .putExtra(Reminders.HAS_ALARM, true) 
        .putExtra(Reminders.METHOD, Reminders.METHOD_DEFAULT); 
        startActivity(intent); 
      } else { 
       Intent intent = new Intent(Intent.ACTION_EDIT); 
       intent.setType("vnd.android.cursor.item/event"); 
       intent.putExtra("title", "Title"); 
       intent.putExtra("description", "Description"); 
       intent.putExtra("beginTime", startMillis); 
       intent.putExtra("allDay", false); 
       intent.putExtra("endTime", endTime.endMillis); 
       intent.putExtra("eventLocation", "Location"); 
       intent.putExtra("hasAlarm", 1); 
       startActivity(intent); 
      }    
     } 
    });  
} 
+0

您是否檢查過[此示例?](http://www.androiddevelopersolution.com/2013/05/android-calendar-sync.html) –

+0

這不是一個自定義日曆,只需添加一些預定義的事件,它不會爲我工作 – Sun

+0

你有相同的任何解決方案.. ??我也必須在我的項目中實施它... – Bhavna

回答

0

如果妳希望直接添加事件到日曆,即沒有打開的意圖,ü可以使用下面的代碼..,

try 
    { 
    Calendar calendarStart = getCalendarFromISO(startDate); 
    Calendar calendarEnd = getCalendarFromISO(endDate); 

    Calendar cal = Calendar.getInstance(); 
    cal.setTimeZone(TimeZone.getTimeZone("GMT-1")); 

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

     values.put(Events.DTSTART, calendarStart.getTimeInMillis()); 
     values.put(Events.DTEND, calendarEnd.getTimeInMillis()); 
     values.put(Events.TITLE, title); 
//  values.put(Events.DESCRIPTION, description); 
     values.put(Events.CALENDAR_ID, 1); 
//  values.put("rrule", "FREQ=DAILY"); 
//  values.put(Events.EVENT_LOCATION, location); 
     values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID()); 

// Provider for Calendar 
if (android.os.Build.VERSION.SDK_INT <= 7) { 

     eventsUri = Uri.parse("content://calendar/events"); 
    } else 
    { 

     eventsUri = Uri.parse("content://com.android.calendar/events"); 
    } 
     Uri uri = cr.insert(eventsUri, values); 

//Returns the id for last added event 
     eventID = Long.parseLong(uri.getLastPathSegment()); 
    Log.v("Calendar", "Calender Event Added" + eventID); 

    } 
catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 

     } 

//格式化日期的方法

public static Calendar getCalendarFromISO(String dateString) { 
    //dateString = dateString.trim().replaceAll(":00$", "00"); // Changing format for SimpleDateFormat 

    Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault()); 
    SimpleDateFormat dateformat = new SimpleDateFormat(ISO8601DATEFORMAT, Locale.getDefault()); 

    try { 
     Date date = dateformat.parse(dateString); 
     calendar.setTime(date); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    return calendar; 
    } 
+0

未解決..... – Sun

0

與此

if (Build.VERSION.SDK_INT >= 14) { 
    Intent intent = new Intent(Intent.ACTION_EDIT).setData(Events.CONTENT_URI) 
     .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis()) 
     .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis()).putExtra(Events.TITLE, eventname) 
     .putExtra(Events.DESCRIPTION, strDesctription) 
     .putExtra(Events.EVENT_LOCATION, eventLoc).putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY) 
     .putExtra(Reminders.HAS_ALARM, true).putExtra(Reminders.METHOD, Reminders.METHOD_DEFAULT).putExtra(Reminders.DURATION, 45) 
     .putExtra(Reminders.MINUTES, 60); 
     mContext.startActivity(intent); 
} else { 
    Intent intent = new Intent(Intent.ACTION_EDIT); 
    intent.setType("vnd.android.cursor.item/event"); 
    intent.putExtra("title", eventname); 
    intent.putExtra("description", strDesctription); 
    intent.putExtra("beginTime", beginTime.getTimeInMillis()); 
    intent.putExtra("allDay", false); 
    intent.putExtra("endTime", endTime.getTimeInMillis()); 
    intent.putExtra("eventLocation", eventLoc); 
    intent.putExtra("hasAlarm", 1); 
    mContext.startActivity(intent); 
} 
+0

仍然沒有解決低版本,請參閱我更新的代碼,使用你的 - 越來越不幸的應用程序已停止 – Sun

+0

我用這與最低SDK 8ñ作品 –

+0

讓我知道它導致問題的地方..我可以進一步幫助你 –

0

嘗試無活動處理意向{行動= android.intent.action.EDIT 典型值= vnd.android你得到

事實.cursor.item/event

使用該特定設備意味着沒有應用程序在其上註冊,可以處理意圖。期。

您可以做的最好的做法是捕獲異常並向用戶顯示一條消息,告訴他們無法在他/她的設備上處理該操作。

你可以做的其他事情是建議安裝一個可以處理它的應用程序,即日曆類型的應用程序。

相關問題