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

これは、Android のバージョン (API レベル) によってカレンダーコンテンツの Uri が異なるためです。このコードを試して、それぞれの 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

これは私の個人的な Cal​​endarOrganizer クラスです。アイスクリーム サンドイッチからカレンダーにアクセスする方法が変更されました。実際、アイス クリーム サンドイッチの前に、Google カレンダーが変更されたり、インストールされていない可能性があるため、オンライン サービスを使用してカレンダーを更新することをお勧めします。

編集:インテントの問題を処理する必要があることを学びましたが、アイスクリームサンドイッチの一部の電話は 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

これが私のために働いたコードです

マニフェストでアクティビティを作成する必要はありません。

すべての Android プラットフォームで動作します。

今月のカレンダービューを表示します

            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 に答える
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 に答える