gpt4 book ai didi

android - 如何自动将事件添加到日历?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:40:15 27 4
gpt4 key购买 nike

我如何自动将事件添加到日历?如果我使用此源代码,它会打开带有填充条目的日历,但我必须单击“保存”按钮才能保存它。

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

}


}

最佳答案

我向您发布了我在市场上的一个应用程序中使用的代码。它会自动将事件添加到用户日历中。它不使用需要用户操作的 Intent。

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

对于 ICS 及更高版本

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 就像

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

关于android - 如何自动将事件添加到日历?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16068082/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com