Open Android calendar with html link

I need to open the calendar application on my Android device using a simple html link. I was able to do this in iOS using href = CALSHOW: // is there something similar for Android?

is there any workaround in the alternative?

Thanks in advance.

+4
source share
2 answers

There is a method on Android that is more powerful than ios url schemes. Intentions And it really is really more powerful!

You can open a calendar or add a calendar event using intent (and many other uses, such as open twitter, share an image on whatsapp, create a note on Evernote ...)

In this link you can find a solution. Take care of android <4.0, because the calendar api is changing and standardizing on Android 4.0. Read this post too

To add a calendar event, you follow the following code: (this is an example, maybe you are not using all of this, or maybe your date format is different)

/** * Creates a calendar event * * @param ce calendar event information * @return */ protected static boolean createCalendarEvent(Context context, CalendarEventCustomObject ce) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) return createCalendarEventIceCream(context, ce); else return createCalendarEventNormal(context, ce); } @SuppressLint("SimpleDateFormat") protected static boolean createCalendarEventNormal(Context context, CalendarEventCustomObject ce) { try { Intent calIntent = new Intent(Intent.ACTION_EDIT); calIntent.setType("vnd.android.cursor.item/event"); calIntent.putExtra("title", ce.description); calIntent.putExtra("eventLocation", ce.location); calIntent.putExtra("description", ce.summary); calIntent.putExtra("calendar_id", ce.id); // Add date info SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ"); Long start = Utils.parseDate(ce.start, df); if (start == null) { start = Utils.parseDate(ce.start, df2); if (start == null) start = System.currentTimeMillis(); } Long end = Utils.parseDate(ce.end, df); if (end == null) { end = Utils.parseDate(ce.end, df2); if (end == null) end = System.currentTimeMillis(); } calIntent.putExtra("beginTime", start); calIntent.putExtra("endTime", end); context.startActivity(calIntent); return true; } catch (Exception e) { return false; } } @SuppressLint("NewApi") protected static boolean createCalendarEventIceCream(Context context, CalendarEventCustomObject ce) { try { Intent calIntent = new Intent(Intent.ACTION_INSERT); calIntent.setType("vnd.android.cursor.item/event"); // Create intent and add string info calIntent.putExtra(Events.TITLE, ce.description); calIntent.putExtra(Events.EVENT_LOCATION, ce.location); calIntent.putExtra(Events.DESCRIPTION, ce.summary); calIntent.putExtra(Events.CALENDAR_ID, ce.id); // add date info SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ"); SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); Long start = Utils.parseDate(ce.start, df); if (start == null) { start = Utils.parseDate(ce.start, df2); if (start == null) start = System.currentTimeMillis(); } Long end = Utils.parseDate(ce.end, df); if (end == null) { end = Utils.parseDate(ce.end, df2); if (end == null) end = System.currentTimeMillis(); } calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, start); calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end); calIntent.setData(CalendarContract.Events.CONTENT_URI); ((Activity) context).startActivity(calIntent); return true; } catch (Exception e) { return false; } } 
+1
source

I don’t know how to be such a link that WebView will support, but it’s very easy to handle this link for myself. I assume that you are loading web content (html) into a WebView . Inside html you will have this type of link:

 <a href="CALSHOW://">Open calendar</a> 

In Java code, you rewrite this link:

 myWebView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if(url.startsWith("CALSHOW")) { openCalendar(); return true; } return super.shouldOverrideUrlLoading(view, url); } }); 

And the calendar opening method:

 protected void openCalendar() { Intent calendarIntent = new Intent() ; /** * Set the time you need ... * */ //calendarIntent.putExtra("beginTime", your_time); calendarIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); calendarIntent.setClassName("com.android.calendar","com.android.calendar.AgendaActivity"); startActivity(calendarIntent); } 

This is a workflow.

-1
source

Source: https://habr.com/ru/post/1487243/


All Articles