用html链接打开android日历

时间:2013-06-20 09:02:08

标签: android html hyperlink calendar href

我需要在Android设备上打开日历应用 使用简单的HTML链接。 我能够在iOS中使用href = CALSHOW:// android有什么类似的东西吗?

另外还有解决方法吗?

提前致谢。

2 个答案:

答案 0 :(得分:0)

在Android上,有一种方法比ios urls架构更强大。 意图 它确实非常强大!

您可以打开日历或使用Intents添加日历事件(以及许多其他用途,例如打开twitter,在whatsapp上共享图像,在evernote上创建一个注释......)

this link中,您可以找到解决方案。小心android< 4.0因为日历的api在Android 4.0上更改并标准化。 Read this this post too

要添加日历事件,请按照下一个代码进行操作:(这是一个示例,也许您没有使用所有这些或者您的日期格式可能不同)

/**
 * 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 :(得分:-1)

我不知道WebView会支持这样的链接,但是为自己处理这个链接非常容易。我假设你正在WebView加载网页内容(html)。 在html中你将有这种类型的链接:

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

在Java代码中,您将覆盖该链接:

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

打开日历的方法:

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);
    }

这就是解决方法