使用Intent将事件插入日历时出错

时间:2012-03-29 13:55:01

标签: android android-intent calendar

我正在尝试从片段中向calendar插入事件,但我一直收到一条错误,即没有找到任何活动来处理Intent。

这是错误 -

  

android.content.ActivityNotFoundException:找不到处理Intent的活动{act = android.intent.action.INSERT cat = [android.intent.category.APP_CALENDAR] typ = vnd.android.cursor.item / event(有附加内容) )}

这是我的代码:

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.addCategory(Intent.CATEGORY_APP_CALENDAR);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(Events.TITLE, "Phototherapy Treatment");
intent.putExtra(Events.EVENT_LOCATION, "");
intent.putExtra(Events.DESCRIPTION, "Phototherapy Treatment");

// Setting dates
Calendar calDate = Calendar.getInstance();
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,calDate.getTimeInMillis());
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
calDate.getTimeInMillis()+60*60*1000);

// Make it a full day event
intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false);

// Make it a recurring Event                    
intent.putExtra(Events.RRULE, "FREQ=WEEKLY;COUNT="+Integer.valueOf(No.getText().toString())+";"
+"WKST=SU;BYDAY="+days);

// Making it private and shown as busy
 intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);
 intent.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);

startActivity(intent);

意图过滤器

<intent-filter> 
<action android:name="android.intent.action.MAIN" /> 
<action android:name="android.intent.action.INSERT" /> 
<category android:name="android.intent.category.APP_CALENDAR" /> 
<data android:mimeType="vnd.android.cursor.item/event" /> 
</intent-filter>

4 个答案:

答案 0 :(得分:13)

我使用了Intent intent = new Intent(Intent.ACTION_EDIT);它似乎解决了这个问题

答案 1 :(得分:1)

某些设备可能未安装日历应用程序。因此请在try catch块中包含以进行检查。

Calendar startTymVar = Calendar.getInstance();
startTymVar.set(2015, 12, 31, 11, 30);

Intent calendarIntentVar = new Intent(Intent.ACTION_INSERT)
            .setData(CalendarContract.Events.CONTENT_URI)
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTymVar.getTimeInMillis())
            .putExtra(CalendarContract.Events.TITLE, "New Year")
            .putExtra(CalendarContract.Events.DESCRIPTION, "Hav ful fun");

try
{
    startActivity(calendarIntentVar);
}
catch (ActivityNotFoundException ErrVar)
{
    Toast.makeText(this, "Install Calender App", Toast.LENGTH_LONG).show();
}

答案 2 :(得分:0)

如果你已经知道它看起来像你做的信息,你不需要启动日历应用程序,只需将事件插入数据库see adding events

同时查看文档APP_CALENDAR仅用于ACTION_MAIN而不是ACTION_INSERT

答案 3 :(得分:0)

检查是否有任何活动可以解决此意图

if (intent.resolveActivity(getPackageManager()) == null) {
}