4

我只是想Calendar Activity从我的Activity. 我的代码中有以下代码Button OnClickListener

Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setData(Uri.parse("content://com.android.calendar/events/"));  
startActivity(calIntent);

但是当我单击 时Button,我的设备挂起,然后我必须强制关闭没有响应的应用程序。

4

5 回答 5

1

这是因为日历内容 Uri 因 android 的不同版本(API 级别)而异。尝试此代码以获取相应 API 级别的日历 Uri。

/*
  * Determines if it's a pre 2.1 or a 2.2 calendar Uri, and returns the Uri
  */
 private String getCalendarUriBase(Context con) {
     String calendarUriBase = null;
     Uri calendars = Uri.parse("content://calendar/calendars");
     Cursor managedCursor = null;
     try {
         managedCursor = managedQuery(calendars, null, null, null, null);
     } catch (Exception e) {
         // eat
     }

     if (managedCursor != null) {
         calendarUriBase = "content://calendar/";
     } else {
         calendars = Uri.parse("content://com.android.calendar/calendars");
         try {
             managedCursor = managedQuery(calendars, null, null, null, null);
         } catch (Exception e) {
             // statement to print the stacktrace
         }

         if (managedCursor != null) {
             calendarUriBase = "content://com.android.calendar/";
         }

     }

     return calendarUriBase;
 }
于 2012-06-13T06:40:44.610 回答
1

这是我个人的 CalendarOrganizer 课程,他们改变了如何从冰淇淋三明治访问日历,事实上,在冰淇淋三明治之前,建议使用他们的在线服务来更新日历,因为谷歌日历可能会被更改甚至没有安装。

编辑:我了解到我需要处理意图问题,但冰淇淋三明治上的某些手机会从 Intent.ACTION_INSERT 而不是 Intent.ACTION_EDIT 崩溃。因此,我更新了我的实现。感谢这篇文章的解决方案。

import android.content.Context;
import android.content.Intent;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;

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) {
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < ICE_CREAM_BUILD_ID) {
            // all SDK below ice cream sandwich
            Intent intent = new Intent(Intent.ACTION_EDIT);
            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");

            try {
                context.startActivity(intent);
                return true;
            } catch(Exception e) {
                return false;
            }
        } else {
            // ice cream sandwich and above
            Intent intent = new Intent(Intent.ACTION_EDIT);
            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;
            }
        }
    }
}
于 2012-06-13T06:42:04.517 回答
0

作为记录,ICS 日历意图记录在这里

http://developer.android.com/guide/topics/providers/calendar-provider.html#intents

于 2012-06-30T03:30:05.747 回答
0

这是对我有用的代码(使用> v4):

Uri uri = Uri.parse("content://com.android.calendar/events");
Intent calIntent = new Intent("android.intent.action.INSERT", uri)
    .setAction(Intent.ACTION_INSERT);
startActivity(calIntent);
于 2012-06-13T06:40:57.480 回答
0

这是对我有用的代码

无需在清单中创建活动。

适用于所有安卓平台。

将显示当月的日历视图

            long epoch = new Date.getTime();
            Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
            builder.appendPath("time");
            ContentUris.appendId(builder, epoch);
            Intent intent = new Intent(Intent.ACTION_VIEW)
                    .setData(builder.build());
            startActivity(intent);
于 2016-03-03T22:27:45.533 回答