Opening Android App from Calendar

后端 未结 2 1251
星月不相逢
星月不相逢 2020-12-09 20:12

The app I\'m working on needs to be able to create events in a chosen Calendar and then when the user views these events in their calendar viewing app it offers an option to

相关标签:
2条回答
  • 2020-12-09 21:02

    If targeting Jelly Bean (API 16+) is acceptable then using CUSTOM_APP_PACKAGE is the best solution. When adding the new calendar event, you just need to fill the CUSTOM_APP_PACKAGE and CUSTOM_APP_URI fields (with your package name and an URI identifying the event respectively):

    ContentValues values = new ContentValues();
    values.put(CalendarContract.Events.CALENDAR_ID, 1);
    values.put(CalendarContract.Events.TITLE, "Check stackoverflow.com");
    values.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis());
    values.put(CalendarContract.Events.DTEND, endTime.getTimeInMillis());
    values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
    
    values.put(CalendarContract.Events.CUSTOM_APP_PACKAGE, getPackageName());
    values.put(CalendarContract.Events.CUSTOM_APP_URI, "myAppointment://1");
    
    getContentResolver().insert(CalendarContract.Events.CONTENT_URI, values);       
    

    Then you need to specify as part of AndroidManifest.xml (as the documentation explains) the Activity that will be called from the Calendar app to show the detailed view, e.g.

        <activity android:name=".ShowCalendarDetailActivity">
            <intent-filter>
                <action android:name="android.provider.calendar.action.HANDLE_CUSTOM_EVENT" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.item/event" />
            </intent-filter>            
        </activity>
    

    ShowCalendarDetailActivity will be started when tapping on the button that appears, and will be passed an Intent with action "android.provider.calendar.action.HANDLE_CUSTOM_EVENT" and its URI will be the calendar item URI.

    The custom URI you supplied is in the extras, with key CalendarContract.EXTRA_CUSTOM_APP_URI.

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        String myCustomUri = getIntent().getStringExtra(CalendarContract.EXTRA_CUSTOM_APP_URI);
    
        ...
    }
    

    If you want to take a look at the code where the Calendar app builds this intent, see EventInfoFragment.updateCustomAppButton() in EventInfoFragment.java.

    0 讨论(0)
  • 2020-12-09 21:16

    The first option you listed works fine on my Nexus S. I just tried it right now to make sure it was working coming from the default Google Calendar app.

    In my case, I'm not intercepting myapp://event/1000, I'm intercepting an actual url more like https://myspecialdomain.com/blahblah. The browser starts to load, but before it has the time to load anything, it gets swapped out by my application.

    I can show you my code tomorrow if you want.

    0 讨论(0)
提交回复
热议问题