打开android日历与html链接 [英] open android calendar with html link

查看:570
本文介绍了打开android日历与html链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用简单的html链接在我的Android设备上打开日历应用程序

我可以在iOS中使用href = CALSHOW://
做类似的安卓吗?



解决方法?



提前感谢。

解决方案

一种比ios urls模式更强大的方法。

您可以打开日历或添加日历事件



此链接,您可以找到解决方案。小心使用Android< 4.0因为日历更改和标准化的api在Android 4.0。 也请阅读此信息



要添加日历活动请遵循下面的代码:(这是一个例子,也许你不使用所有这一切,或者你的日期格式是不同的)

  / ** 
*创建一个日历事件
*
* @param ce日历事件信息
* @return
* /
protected static boolean createCalendarEvent上下文上下文,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.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);

//添加日期信息
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);

//创建intent并添加字符串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);

//添加日期信息
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;
}
}


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

in alternative is there any workaround?

Thanks in advance.

解决方案

On Android there are a method more powerful than the ios urls schemas. Intents And it is really really more powerful!

You can open the calendar or add a calendar event with the Intents (and a lot of other uses like open twitter, share an image on whatsapp, create a note on evernote ... )

In this link you can find the solution. Take care with android <4.0 because the api of calendar changes and standardizes on Android 4.0. Read this this post too

To add a calendar event you follow the next code: (this is an example, maybe you not use all of this or perhaps 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;
    }
}   

这篇关于打开android日历与html链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆