android studio about inserting event on own mobile calendar -
the programme runs error. however, event doesnt insert own mobile's calendar. know reason that?? mobile pentech a880. or there method insert events?
package com.example.lean.myapplication; public class myactivity extends activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.list_fragment); button button = (button) findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(intent.action_pick, android.provider.mediastore.images.media.external_content_uri); startactivityforresult(intent, 2); } } ); addtocalendar(getapplicationcontext(), "hello","2015/10/10 10:05:33" ); } public static void addtocalendar(context ocontext, final string title, final string eventstartdate) { string eventuristring = null; long startdate = new date(eventstartdate).gettime(); long enddate = new date(eventstartdate).gettime() + 1000 * 60 * 60; // next 1hr timezone timezone = timezone.getdefault(); contentvalues eventvalues = new contentvalues(); if (build.version.sdk_int >= 1 && build.version.sdk_int < 13) { eventuristring = "content://com.android.calendar/events"; eventvalues.put("calendar_id", 1); eventvalues.put("title", title); eventvalues.put("description", ""); eventvalues.put("eventlocation", ""); eventvalues.put("dtstart", startdate); eventvalues.put("dtend", enddate); eventvalues.put("eventtimezone", timezone.getdefault().getid()); eventvalues.put("eventstatus", ""); eventvalues.put("visibility", 3); eventvalues.put("transparency", 0); eventvalues.put("hasalarm", 1); uri eventuri = ocontext.getapplicationcontext().getcontentresolver().insert(uri.parse("content://com.android.calendar/events"), eventvalues); long eventid = long.parselong(eventuri.getlastpathsegment()); string reminderuristring = "content://com.android.calendar/reminders"; contentvalues remindervalues = new contentvalues(); remindervalues.put("event_id", eventid); remindervalues.put("minutes", 5); remindervalues.put("method", 1); ocontext.getapplicationcontext().getcontentresolver().insert(uri.parse(reminderuristring), remindervalues); } else if (build.version.sdk_int >= 14 ) { eventvalues.put(calendarcontract.events.dtstart, startdate); eventvalues.put(calendarcontract.events.dtend, enddate); eventvalues.put(calendarcontract.events.title, title); eventvalues.put(calendarcontract.events.description, ""); eventvalues.put(calendarcontract.events.calendar_id, 3); eventvalues.put(calendarcontract.events.event_timezone, timezone.getid()); ocontext.getapplicationcontext().getcontentresolver().insert(uri.parse("content://com.android.calendar/events"), eventvalues); } toast.maketext(ocontext, "event created on : " + startdate, toast.length_short).show(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.my, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
}
perhaps didn't ask permissions
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"...> ... <uses-permission android:name="android.permission.read_calendar" /> <uses-permission android:name="android.permission.write_calendar" /> ... </manifest>
Comments
Post a Comment