CalendarContract:Column' dtstart'无效(使用injectCalendar())

时间:2016-11-26 02:24:15

标签: java android android-studio

我一直在编写一个类来生成内容来测试Android设备上的应用程序,但我一直遇到这个错误。我对Java很新,所以我不习惯解释这些运行时错误。任何帮助将不胜感激!我认为我的injectCalendar方法是导致这种情况但我不太确定......

import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;
import android.provider.UserDictionary;
import android.telephony.SmsManager;
import android.provider.ContactsContract.RawContacts;
import android.provider.Browser;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Contacts.Data;
import android.content.Intent;
import android.text.format.Time;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;



class ContentInjector {

    private Context mContext;
    ContentInjector(Context context) { mContext = context; }

    void injectSMS(String phone_number) {
        String text_message = "hello world";
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phone_number, null, text_message, null, null);

    }

    void injectContact() {
        ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
        op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                //.withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DEFAULT)
                .build());

        // first and last names
        op_list.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.GIVEN_NAME, "Second Name")
                .withValue(StructuredName.FAMILY_NAME, "First Name")
                .build());

        op_list.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "09876543210")
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_HOME)
                .build());
        op_list.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)

                .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Email.DATA, "abc@xyz.com")
                .build());

        try{
             mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    void injectMMS(){}

    void injectDict(){
        UserDictionary.Words.addWord(mContext, "most frequently used word", 250, null, Locale.getDefault());
    }

    void injectCalendar() {


        long startMillis = 0;
        long endMillis = 0;
        Calendar beginTime = Calendar.getInstance();
        beginTime.set(2016, 9, 14, 7, 30);
        startMillis = beginTime.getTimeInMillis();
        Calendar endTime = Calendar.getInstance();
        endTime.set(2016, 9, 14, 8, 45);
        endMillis = endTime.getTimeInMillis();
        String timeZone = TimeZone.getDefault().getID();

        Uri calUri = CalendarContract.Calendars.CONTENT_URI;
        ContentValues cv = new ContentValues();
        cv.put(CalendarContract.Calendars.ACCOUNT_NAME, "CIFD ACCOUNT NAME");
        cv.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
        cv.put(CalendarContract.Calendars.NAME, "CIFD'S NAME");
        cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "CIFD CALENDAR DISPLAY NAME");
        cv.put(CalendarContract.Calendars.OWNER_ACCOUNT, true);
        cv.put(CalendarContract.Calendars.VISIBLE, 1);
        cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
        cv.put(CalendarContract.Events.CALENDAR_ID, 1);
        cv.put(CalendarContract.Events.DTSTART, startMillis);
        cv.put(CalendarContract.Events.DTEND, endMillis);
        cv.put(CalendarContract.Events.TITLE, "CIFD CALENDAR TITLE");
        cv.put(CalendarContract.Events.DESCRIPTION, "CFID CALENDAR DESCRIPTION");
        cv.put(CalendarContract.Events.EVENT_LOCATION, "CIFD CALENDAR LOCATION");
        cv.put(CalendarContract.Events.ALL_DAY, 0);   // 0 for false, 1 for true
        cv.put(CalendarContract.Events.HAS_ALARM, 1); // 0 for false, 1 for true


        cv.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);

        calUri = calUri.buildUpon()
                .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "CIFD ACCOUNT NAME")
                .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
                .build();
        mContext.getContentResolver().insert(calUri, cv);

    }


    private String getCalendarUriBase(Context context) {

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


    void injectBookmark(){}

    void injectSearch(){

    }


}

这是我在初始化类并运行每个方法后从logcat接收的输出。

11-25 21:05:34.589 16547-16547/com.mydata.contactlist I/LoadedApk: No resource references to update in package common
11-25 21:05:34.590 16547-16547/com.mydata.contactlist I/LoadedApk: No resource references to update in package com.cyngn.hexo
11-25 21:05:34.591 16547-16547/com.mydata.contactlist I/LoadedApk: No resource references to update in package com.cyngn.hexo
11-25 21:05:34.706 16547-16547/com.mydata.contactlist W/ResourceType: For resource 0x01030224, entry index(548) is beyond type entryCount(9)
11-25 21:05:34.706 16547-16547/com.mydata.contactlist W/ResourceType: For resource 0x01030224, entry index(548) is beyond type entryCount(9)
11-25 21:05:34.759 16547-16547/com.mydata.contactlist W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-25 21:05:34.852 16547-16566/com.mydata.contactlist D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-25 21:05:34.858 16547-16547/com.mydata.contactlist D/Atlas: Validating map...
11-25 21:05:34.888 16547-16566/com.mydata.contactlist I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.01.00.042.030_msm8974_LA.BF.1.1.1_RB1__release_AU ()
                                                                    OpenGL ES Shader Compiler Version: E031.25.03.06
                                                                    Build Date: 04/15/15 Wed
                                                                    Local Branch: mybranch9068252
                                                                    Remote Branch: quic/LA.BF.1.1.1_rb1.19
                                                                    Local Patches: NONE
                                                                    Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.01.00.042.030 +  NOTHING
11-25 21:05:34.888 16547-16566/com.mydata.contactlist I/OpenGLRenderer: Initialized EGL, version 1.4
11-25 21:05:34.903 16547-16566/com.mydata.contactlist D/OpenGLRenderer: Enabling debug mode 0
11-25 21:05:34.924 16547-16547/com.mydata.contactlist I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2f9a9c54 time:6404104
11-25 21:05:36.807 16547-16547/com.mydata.contactlist I/Timeline: Timeline: Activity_launch_request id:com.mydata.contactlist time:6405987
11-25 21:05:37.172 16547-16547/com.mydata.contactlist I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@32dc9aac time:6406351
11-25 21:05:41.262 16547-16662/com.mydata.contactlist E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                        Process: com.mydata.contactlist, PID: 16547
                                                                        java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                            at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                                            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                            at java.lang.Thread.run(Thread.java:818)
                                                                         Caused by: java.lang.IllegalArgumentException: column 'dtstart' is invalid
                                                                            at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
                                                                            at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
                                                                            at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
                                                                            at android.content.ContentResolver.insert(ContentResolver.java:1207)
                                                                            at com.mydata.contactlist.ContentInjector.injectCalendar(ContentInjector.java:123)
                                                                            at com.mydata.contactlist.MainActivity$ExportThread.doInBackground(MainActivity.java:166)
                                                                            at com.mydata.contactlist.MainActivity$ExportThread.doInBackground(MainActivity.java:158)
                                                                            at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                            at java.lang.Thread.run(Thread.java:818) 

1 个答案:

答案 0 :(得分:0)

它应该是CalendarContract.EventsColumns.DTSTART吗?尝试仅使用CalendarContract.Events.DTSTART替换"dtstart",看看会发生什么。

https://developer.android.com/reference/android/provider/CalendarContract.EventsColumns.html#DTSTART