如果您有多个日历,如何创建活动 - Android

时间:2015-01-22 08:49:23

标签: android calendar android-calendar

我正在尝试创建有问题的日历活动。下面的代码是工作文件,但问题是当我启动活动时它弹出两个日历(com.android.calendar,com.google.android.calendar)。

如何选择任何一个?

Calendar cal = Calendar.getInstance();              
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", cal.getTimeInMillis());
        intent.putExtra("allDay", true);
        intent.putExtra("rrule", "FREQ=YEARLY");
        intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
        intent.putExtra("title", "A Test Event from android app");
        startActivity(intent);

2 个答案:

答案 0 :(得分:0)

您似乎已在设备上安装了两个日历应用程序但通常情况并非如此,通常设备默认情况下会有一个日历应用程序。

我建议你不要因为以下原因在这种情况下做任何事情。

1) Generally device having only one calendar app
2) If you choose a particular calendar then the devices which not installed your specified calendar won't able to use you app.
3) If person has installed two calendar let him decide which he want to use to add event, which is recommended by Google.

答案 1 :(得分:0)

您可以创建一个选择器,并允许用户选择要保存事件的日历应用。

Intent.createChooser(intent, "Choose Calendar");

您使用修改代码:

Calendar cal = Calendar.getInstance();              
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(Intent.createChooser(intent, "Choose Calendar"));