2

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; 

}

}

4

1 に答える 1

0

リマインダーを挿入するために「calendar_timezone」を追加しないでください。これにより、例外が発生します。

「event_timezone」で試してください。

于 2015-08-06T11:33:24.630 に答える