如何从我的应用程序中打开日历?

时间:2011-02-17 21:18:15

标签: java android android-intent

我正在构建一个Android应用程序,我不需要我的应用程序让日历成为它的一部分(加上它会打败目的),我的应用程序允许用户收听广播电台,并需要选项将事件设置为(记得在特定时间收听广播),如果应用程序有自己的日历,那么只有当用户打开应用程序时,事件警报才会消失...毫无意义。我一直在寻找,找不到,有没有办法使用意图或其他东西打开谷歌日历或Android可能有的其他日历? 我需要把我的意图(/其他代码)放在我已经拥有的监听器中,现在看起来像这样

private View.OnClickListener reminder = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                  // open calendar code goes here.

            }
};

我不需要应用程序预先填写日历中的任何字段只需打开它,我将其余部分留给用户。 欢迎大家帮忙,谢谢

1 个答案:

答案 0 :(得分:9)

如果您只是想打开日历,可以使用这些组件名称并使用这些组件名称(如果您想支持旧手机,则可能需要兼顾两者)

Intent i = new Intent();

//Froyo or greater (mind you I just tested this on CM7 and the less than froyo one worked so it depends on the phone...)
cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");

//less than Froyo
cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");

i.setComponent(cn);
startActivity(i);

如果您想要转到添加事件屏幕(听起来更适合您的目的),请使用以下内容:

 //all version of android
 Intent i = new Intent();

 // mimeType will popup the chooser any  for any implementing application (e.g. the built in calendar or applications such as "Business calendar"
 i.setType("vnd.android.cursor.item/event"); 

 // the time the event should start in millis. This example uses now as the start time and ends in 1 hour
 i.putExtra("beginTime", new Date().getTime()); 
 i.putExtra("endTime", new Date().getTime() + DateUtils.HOUR_IN_MILLIS);

 // the action
 i.setAction(Intent.ACTION_EDIT);
 startActivity(i);

(代码未经测试,从现有项目中复制)

相关问题