Opening Android App from Calendar

后端 未结 2 1257
星月不相逢
星月不相逢 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.

        
            
                
                
                
                        
        
    

    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.

提交回复
热议问题