4

我需要使用简单的 html 链接在我的 Android 设备上打开日历应用程序。我可以在 iOS 中使用 href=CALSHOW:// 做到这一点,Android 有类似的东西吗?

或者有什么解决方法吗?

提前致谢。

4

2 回答 2

0

在 Android 上,有一种比 ios urls 模式更强大的方法。Intents而且真的真的更厉害!

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

此链接中,您可以找到解决方案。请注意android <4.0,因为日历的api在Android 4.0上发生了变化和标准化。也阅读这篇文章

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

/**
 * 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;
    }
}   
于 2013-06-20T09:20:08.203 回答
-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);
    }

这就是解决方法

于 2013-06-20T09:16:17.053 回答