我如何可以自动添加事件到日历? [英] How can I add event to the calendar automatically?

查看:285
本文介绍了我如何可以自动添加事件到日历?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以自动添加事件到日历?如果我用这个源$ C ​​$ C,它打开日历与填充项,但我必须点击Save按钮保存。

 公共类CalendarApplicationActivity扩展活动
{

公共静态最后的String [] EVENT_PROJECTION =新的String []
{
        Calendars._ID,// 0
        Calendars.ACCOUNT_NAME,// 1
        Calendars.CALENDAR_DISPLAY_NAME // 2
};


私有静态最终诠释PROJECTION_DISPLAY_NAME_INDEX = 2;

@覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_calendar_application);
}

公共无效的onClick(视图查看)
{

    意向意图=新的意图(Intent.ACTION_INSERT);
    intent.setType(vnd.android.cursor.item /事件);
    intent.putExtra(Events.TITLE,学习安卓);
    intent.putExtra(Events.EVENT_LOCATION,家居服之家);
    intent.putExtra(Events.DESCRIPTION,下载示例);

    GregorianCalendar的calDate =新的GregorianCalendar(2012,10,02);
    intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
            calDate.getTimeInMillis());
    intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
            calDate.getTimeInMillis());

    intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY,真正的);


    intent.putExtra(Events.ACCESS_LEVEL,Events.ACCESS_PRIVATE);
    intent.putExtra(Events.AVAILABILITY,Events.AVAILABILITY_BUSY);

    startActivity(意向);

}


}
 

解决方案

我张贴您在使用我在市场上的应用程序之一,这code。它增加了自动事件给用户日历。它不使用意图,需要一个用户操作。

 公共无效的addEvent(CalendarEvent EVT){
    //Log.d(Params.LOG_APP,插入事件[+ EVT +]);

    尝试 {
        乌里evtUri = ctx.getContentResolver()插入(getCalendarUri(事件),CalendarEvent.toContentValues​​(EVT))。
        Log.d(Params.LOG_APP,+ evtUri);
    }
    捕获(的Throwable T){
        //Log.e(Params.LOG_APP,,吨);
    }
}

公共无效setContext(上下文的背景下){
    this.ctx =背景;
    this.baseUri = getCalendarUriBase();
}

私人乌里getCalendarUri(字符串路径){
    返回Uri.parse(基本URI +/+路径);
}

私人字符串getCalendarUriBase(){
    字符串calendarUriBase = NULL;
    乌里日历= Uri.parse(内容://日历/日历);
    光标managedCursor = NULL;
    尝试 {
        managedCursor = ctx.getContentResolver()查询(日历,NULL,NULL,NULL,NULL);
    }赶上(例外五){
       // e.printStackTrace();
    }

    如果(managedCursor!= NULL){
        calendarUriBase =内容://日历/;
    } 其他 {
        日历= Uri.parse(内容://com.android.calendar/calendars);
        尝试 {
            managedCursor = ctx.getContentResolver()查询(日历,NULL,NULL,NULL,NULL);
        }赶上(例外五){
           // e.printStackTrace();
        }

        如果(managedCursor!= NULL){
            calendarUriBase =内容://com.android.calendar/;
        }

    }

    Log.d(Params.LOG_APP,URI [+ calendarUriBase +]);
    返回calendarUriBase;
}
 

而对于ICS后来

 公共无效的addEvent(CalendarEvent EVT){

    ContentResolver的CR = context.getContentResolver();
    开放的我们的uri = cr.insert(Events.CONTENT_URI,CalendarEvent.toICSContentValues​​(EVT));
    的System.out.println(事件的URI [+ URI +]);

}
 

CalendarEvent就像

 公共静态ContentValues​​ toContentValues​​(CalendarEvent EVT){
    ContentValues​​ CV =新ContentValues​​();
    cv.put(CALENDAR_ID,evt.getIdCalendar());
    cv.put(标题,evt.getTitle());
    cv.put(描述,evt.getDescr());
    cv.put(eventLocation,evt.getLocation());
    cv.put(DTSTART,evt.getStartTime());
    cv.put(DTEND,evt.getEndTime());
    cv.put(eventStatus,1);
    cv.put(知名度,0);
    cv.put(透明度,0);

    返回简历;

}

公共静态ContentValues​​ toICSContentValues​​(CalendarEvent EVT){

    ContentValues​​ CV =新ContentValues​​();
    cv.put(Events.CALENDAR_ID,evt.getIdCalendar());
    cv.put(Events.TITLE,evt.getTitle());
    cv.put(Events.DESCRIPTION,evt.getDescr());
    cv.put(Events.EVENT_LOCATION,evt.getLocation());
    cv.put(Events.DTSTART,evt.getStartTime());
    cv.put(Events.DTEND,evt.getEndTime());

    日历CAL = Calendar.getInstance();
    时区TZ = cal.getTimeZone();

    cv.put(Events.EVENT_TIMEZONE,tz.getDisplayName());
    / *
    cv.put(Events.STATUS,1);
    cv.put(Events.VISIBLE,0);
    cv.put(透明度,0);

    返回简历;
    * /

    返回简历;
}
 

How can i add event to the calendar automatically? If I use this source code, it opens Calendar with filled entries, but I must click on save button to save it.

public class CalendarApplicationActivity extends Activity
{  

public static final String[] EVENT_PROJECTION = new String[] 
{  
        Calendars._ID, // 0  
        Calendars.ACCOUNT_NAME, // 1  
        Calendars.CALENDAR_DISPLAY_NAME // 2  
};  


private static final int PROJECTION_DISPLAY_NAME_INDEX = 2;  

@Override 
public void onCreate(Bundle savedInstanceState)
{  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_calendar_application);  
}  

public void onClick(View view) 
{  

    Intent intent = new Intent(Intent.ACTION_INSERT);  
    intent.setType("vnd.android.cursor.item/event");  
    intent.putExtra(Events.TITLE, "Learn Android");  
    intent.putExtra(Events.EVENT_LOCATION, "Home suit home");  
    intent.putExtra(Events.DESCRIPTION, "Download Examples");  

    GregorianCalendar calDate = new GregorianCalendar(2012, 10, 02);  
    intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,  
            calDate.getTimeInMillis());  
    intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,  
            calDate.getTimeInMillis());  

    intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);  


    intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);  
    intent.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);  

    startActivity(intent);  

}  


}

解决方案

I post you this code that is used in one of my app in the market. It adds automatically event to the user calendar. It doesn't use Intent that requires an user action.

public void addEvent(CalendarEvent evt) {
    //Log.d(Params.LOG_APP, "Insert event ["+evt+"]");

    try {
        Uri evtUri = ctx.getContentResolver().insert(getCalendarUri("events"), CalendarEvent.toContentValues(evt));
        Log.d(Params.LOG_APP, "" + evtUri);
    }
    catch(Throwable t) {
        //Log.e(Params.LOG_APP, "", t);
    }
}

public void setContext(Context context) {
    this.ctx = context;
    this.baseUri = getCalendarUriBase();
}

private Uri getCalendarUri(String path) {
    return Uri.parse(baseUri + "/" + path);
}

private String getCalendarUriBase() {
    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = null;
    try {
        managedCursor = ctx.getContentResolver().query(calendars, null, null, null, null);
    } catch (Exception e) {
       // e.printStackTrace();
    }

    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = ctx.getContentResolver().query(calendars, null, null, null, null);
        } catch (Exception e) {
           // e.printStackTrace();
        }

        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }

    }

    Log.d(Params.LOG_APP, "URI ["+calendarUriBase+"]");
    return calendarUriBase;
}

And for ICS and later

public void addEvent(CalendarEvent evt) {

    ContentResolver cr = context.getContentResolver();
    Uri uri = cr.insert(Events.CONTENT_URI, CalendarEvent.toICSContentValues(evt));
    System.out.println("Event URI ["+uri+"]");

}

CalendarEvent is like

public static ContentValues toContentValues(CalendarEvent evt) {
    ContentValues cv = new ContentValues();
    cv.put("calendar_id", evt.getIdCalendar());
    cv.put("title", evt.getTitle());
    cv.put("description", evt.getDescr());
    cv.put("eventLocation", evt.getLocation());
    cv.put("dtstart", evt.getStartTime());
    cv.put("dtend", evt.getEndTime());
    cv.put("eventStatus", 1);
    cv.put("visibility", 0);
    cv.put("transparency", 0);

    return cv;

}

public static ContentValues toICSContentValues(CalendarEvent evt) {

    ContentValues cv = new ContentValues();
    cv.put(Events.CALENDAR_ID, evt.getIdCalendar());
    cv.put(Events.TITLE, evt.getTitle());
    cv.put(Events.DESCRIPTION, evt.getDescr());
    cv.put(Events.EVENT_LOCATION, evt.getLocation());
    cv.put(Events.DTSTART, evt.getStartTime());
    cv.put(Events.DTEND, evt.getEndTime());

    Calendar cal = Calendar.getInstance();
    TimeZone tz = cal.getTimeZone();

    cv.put(Events.EVENT_TIMEZONE, tz.getDisplayName());
    /*
    cv.put(Events.STATUS, 1);
    cv.put(Events.VISIBLE, 0);
    cv.put("transparency", 0);

    return cv;
    */

    return cv;
}

这篇关于我如何可以自动添加事件到日历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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