我正在尝试向 android 日历添加一个事件,我的代码在旧版本设备(Android 2.3)上运行良好。但相同的代码在尝试在较新的 android 设备(Android 4.1)中运行时给出“只有提供者可以写入 calendar_timezone ”错误
注意:我不想使用最新的日历 API 添加事件,因为此 API 可用于具有 android 构建 >=14 的设备
以下是错误的详细日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calenderwithreminder/com.example.calenderwithreminder.MainActivity}: java.lang.IllegalArgumentException: Only the provider may write to calendar_timezone
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
android.app.ActivityThread.access$600(ActivityThread.java:141)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:5103)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:525)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(25325): Caused by: java.lang.IllegalArgumentException: Only the provider may write to calendar_timezone
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
android.content.ContentProviderProxy.insert(ContentProviderNative.java:440)
android.content.ContentResolver.insert(ContentResolver.java:914)
com.example.calenderwithreminder.MainActivity.onCreate(MainActivity.java:77)
android.app.Activity.performCreate(Activity.java:5133)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-08 17:42:05.308: E/AndroidRuntime(25325): ... 11 more
我添加事件的代码如下
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* A device may have more than one Calendar configure in it. So first,
* you must find out available calendar's name and their id.
*/
Cursor cursor = getContentResolver() .query(getCalendarURI(false),new String[] { "_id" }, null,null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CNames.length; i++) {
CalIds[i] = cursor.getInt(0);
cursor.moveToNext();
}
Log.e("test","total calenders ="+CalIds.length);
Log.e("test","using calnder id "+CalIds[0]);
// get calendar
Calendar cal = Calendar.getInstance();
Uri EVENTS_URI = getCalendarURI(true);
ContentResolver cr = getContentResolver();
// event insert
Cursor cursor11 = getContentResolver() .query(getCalendarURI(true),null, null,null, null);
cursor11.moveToFirst();
int colCount =cursor11.getColumnCount();
for(int i=0;i<colCount;i++)
{
Log.e("test","--"+cursor11.getColumnName(i));
}
ContentValues values = new ContentValues();
values.put("calendar_id",CalIds[0]);
values.put("title", "Testing Reminder");
values.put("allDay", 0);
values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now
values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
values.put("description", "Testing Reminder description");
//Event Id
// values.put("_id", scheduleId);
//0~ default; 1~ confidential; 2~ private; 3~ public
values.put("visibility", 0);
//0~ false; 1~ true
values.put("hasAlarm", 1);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
values.put("eventStatus", 1);
//0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
values.put("transparency", 0);
if(Build.VERSION.SDK_INT >= 14)
{
java.util.TimeZone timeZone = java.util.TimeZone.getDefault(); //timeZone.getID() //"America/Los_Angeles"
values.put("calendar_timezone", timeZone.getID());
}
Uri event = cr.insert(EVENTS_URI, values);
Toast.makeText(getApplicationContext(), "Event Added successfully", Toast.LENGTH_LONG).show();
// reminder insert
Uri REMINDERS_URI = getReminderURI();
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 24*60 );
cr.insert( REMINDERS_URI, values );
Toast.makeText(getApplicationContext(), "Reminder Added successfully", Toast.LENGTH_LONG).show();
Cursor cursor1 = getContentResolver() .query(getReminderURI(),null, null,null, null);
cursor1.moveToFirst();
int colCount1 = cursor1.getColumnCount();
for(int i=0;i<colCount1;i++)
{
Log.e("test","reminder "+cursor1.getColumnName(i)+"---"+i);
}
}
public void deleteEvent(Uri eventUri)
{
// for deleting event
// getContentResolver().delete(path to the content, want to delete,
// CONDITION, ARGUMENTS);
// CONDITION + ARGUMENTS work as where condition to find a particular
/*event.
getContentResolver().delete(
Uri.parse(getCalendarUriBase(this)),
"calendar_id=? and description=? and eventLocation=? ",
new String[] { String.valueOf(CalIds[0]), "Birthday Party",
"Delhi" });*/
}
private Uri getCalendarURI( boolean eventUri){
Uri calendarURI = null;
if (android.os.Build.VERSION.SDK_INT <= 7 )
{
calendarURI = (eventUri)?Uri.parse("content://calendar/events"):Uri.parse("content://calendar/calendars");
}
else
{
calendarURI = (eventUri)?Uri.parse("content://com.android.calendar/events"): Uri.parse("content://com.android.calendar/calendars");
}
return calendarURI;
}
private Uri getReminderURI(){
Uri reminderURI = null;
if (android.os.Build.VERSION.SDK_INT <= 7 )
{
reminderURI = Uri.parse("content://calendar/reminders");
}
else
{
reminderURI = Uri.parse("content://com.android.calendar/reminders");
}
return reminderURI;
}
}