2013-02-04 5 views
8

Ich weiß, das wurde zuvor in diesem Forum gefragt, aber ich habe alles versucht geschrieben hier und immer noch nichts funktioniert für mich. Ich möchte ein Ereignis in den Kalender einfügen. Da auf meinem Gerät mehrere Kalenderanwendungen vorhanden sind, möchte ich dem Benutzer ermöglichen, zu wählen, welche Kalenderanwendung das neue Ereignis enthalten soll, ähnlich wie wenn der Benutzer versucht, einen Standort mithilfe einer Kartenanwendung anzuzeigen (der Benutzer kann auswählen, ob er dies möchte) aktiviere Google Maps, das Internet, ...). Aus diesem Grund muss ich eine Absicht verwenden.Event in Kalender einfügen mit Absicht

Übrigens ist mir bewusst, dass das Einfügen neuer Ereignisse in den Kalender mit Intents nur auf Geräten mit SDK Version 14 oder höher zulässig ist. Mein Gerät hat eine API-Stufe von 15, daher unterstützt es die Kalender-API.

Hier ist mein Code:

Intent calendarIntent = new Intent(Intent.ACTION_INSERT); 
calendarIntent.setData(Events.CONTENT_URI); 
calendarIntent.putExtra(Events.TITLE, "title"); 
calendarIntent.putExtra(Events.EVENT_LOCATION, "address"); 
Calendar cal = Calendar.getInstance(); 
cal.set(Calendar.DAY_OF_MONTH, 1); 
cal.set(Calendar.MONTH, 0); 
cal.set(Calendar.YEAR, 2013); 
cal.set(Calendar.HOUR_OF_DAY, 20); 
cal.set(Calendar.MINUTE, 0); 
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, cal.getTime().getTime()); 
cal.set(Calendar.HOUR_OF_DAY, 20); 
cal.set(Calendar.MINUTE, 30); 
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, cal.getTime().getTime()); 
ctx.startActivity(calendarIntent); 

ich diese Ausnahme erhalten:

02-04 17:55:23.957: E/AndroidRuntime(3781): FATAL EXCEPTION: main 
02-04 17:55:23.957: E/AndroidRuntime(3781): java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.applicat.meuchedet/com.applicat.meuchedet.MainScreenActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSERT dat=content://com.android.calendar/events (has extras) } 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread.access$600(ActivityThread.java:127) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.os.Looper.loop(Looper.java:137) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread.main(ActivityThread.java:4507) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at dalvik.system.NativeStart.main(Native Method) 
02-04 17:55:23.957: E/AndroidRuntime(3781): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSERT dat=content://com.android.calendar/events (has extras) } 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1535) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1387) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.Activity.startActivityForResult(Activity.java:3190) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at com.applicat.meuchedet.Screen.startActivity(Screen.java:433) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at com.applicat.meuchedet.CalendarAppointmentScheduler.writeAppointmentToCalendar(CalendarAppointmentScheduler.java:137) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at com.applicat.meuchedet.MainScreenActivity.onCreate(MainScreenActivity.java:258) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.Activity.performCreate(Activity.java:4465) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  ... 11 more 

Was mache ich falsch?

Thanx

+0

einfügen Haben Sie die Kalender-App installiert haben? Der Fehler weist darauf hin, dass nichts vorhanden ist, um die Absicht zu behandeln. – BoredAndroidDeveloper

Antwort

11

Sie gefangen ActivityNotFoundException weil jede Aktivität Ihre Aktion nicht verarbeiten kann.

Statt Intent.ACTION_INSERT mit versuchen, wie dies zu tun:

Intent intent = new Intent(Intent.ACTION_EDIT); 
intent.setType("vnd.android.cursor.item/event"); 
intent.putExtra(Events.TITLE, strTitle); 
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, 
        startDateMillis); 
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, 
        endDateMillis); 
intent.putExtra(Events.ALL_DAY, false);// periodicity 
      intent.putExtra(Events.DESCRIPTION,strDescription)); 

Sie in Entwicklerdokumentation für andere Attribute überprüfen.

+0

Ich denke, dass Yvan Sie in die richtige Richtung zeigt, nachdem Sie dies gesehen haben: http://stackoverflow.com/questions/9926830/error-inserting-event-into-calendar-with-intent Offenbar einige Telefone nicht die ACTION_INSERT und Stattdessen benötigen Sie ACTION_EDIT. Ich glaube nicht, dass Sie die setType-Zeile verwenden müssen, aber ich könnte falsch liegen – BoredAndroidDeveloper

+0

Ich verwende immer Yvan's Methode und es funktioniert. – psykhi

+0

Danke! Das ist für mich in Ordnung. Weißt du vielleicht, warum es notwendig ist, die setType-Codezeile zu verwenden? – shai

0

ein Ereignis wie dieses

Calendar beginTime = Calendar.getInstance(); 
beginTime.set(2012, 0, 19, 7, 30); 
Calendar endTime = Calendar.getInstance(); 
endTime.set(2012, 0, 19, 8, 30); 
Intent intent = new Intent(Intent.ACTION_INSERT) 
    .setData(Events.CONTENT_URI) 
    .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, 
beginTime.getTimeInMillis()) 
    .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, 
endTime.getTimeInMillis()) 
    .putExtra(Events.TITLE, "Yoga") 
    .putExtra(Events.DESCRIPTION, "Group class") 
    .putExtra(Events.EVENT_LOCATION, "The gym") 
    .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY) 
    .putExtra(Intent.EXTRA_EMAIL, "[email protected],[email protected]"); 
startActivity(intent); 
Verwandte Themen