android日历提供程序-事件未创建/显示

wlwcrazw  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(428)

我已经写了一个简单的应用程序插入日历事件到一个给定的android日历。代码没有任何错误,但是通风口也没有在给定的日历中显示。我想知道有没有人有什么想法?
我设置了以下权限:

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<permission android:name="android.permission.READ_CALENDAR" />
<permission android:name="android.permission.WRITE_CALENDAR" />
<permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

日历id可通过此功能找到,此功能工作正常,值将在以后通过另一位代码进行存储和调用:

protected void getCalenders()
{
    Cursor cursor;
    cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),
            new String[] { "_id", "calendar_displayName" }, null, null, null);

    String calString = "";
    // Get calendars name
   calString = calString + "Calender count " + cursor.getCount();
    cals = new  ArrayList<String>();

    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        String[] calendarNames = new String[cursor.getCount()];
        // Get calendars id
        int calendarIds[] = new int[cursor.getCount()];
        for (int i = 0; i < cursor.getCount(); i++) {
            cals.add(cursor.getInt(0) + ": " +  cursor.getString(1));
            cursor.moveToNext();
        }
    } else {
       cals.add("0: No calenders found!");
    }

    // Create an ArrayAdapter using the string array and a default spinner layout
    m_adapterForSpinnerWC = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item, cals);
    // Specify the layout to use when the list of choices appears
    m_adapterForSpinnerWC.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    Spinner spn = findViewById(R.id.spCalerders);

    spn.setAdapter(m_adapterForSpinnerWC);

}

事件插入如下:

public class updateCalender extends  AsyncTask
{
    Context curActivityContext = null;
    public  updateCalender(Context currentActivityContext){
        curActivityContext = currentActivityContext;
    }

    public long pushEventToCalender(int calenderID, String title, String addInfo, String place, int status,
                                           long startDate, long endDate, int reminderTime, boolean needReminder,
                                           boolean needMailService) {
        /*****************Event: note(without alert)*******************/

        ContentResolver cr = getContentResolver();
        Calendar beginTime = Calendar.getInstance();

        ContentValues values = new ContentValues();
        values.put(CalendarContract.Events.DTSTART, startDate);
        values.put(CalendarContract.Events.DTEND, endDate);
        values.put(CalendarContract.Events.TITLE, title);
        values.put(CalendarContract.Events.DESCRIPTION,addInfo);
        values.put(CalendarContract.Events.CALENDAR_ID, calenderID);
        values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
        values.put(CalendarContract.Events.EVENT_LOCATION, place);
        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
        long eventID = Long.parseLong(uri.getLastPathSegment());

        // Force a sync
        Bundle extras = new Bundle();
        extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        AccountManager am = AccountManager.get(getApplicationContext());
        Account[] acc = am.getAccountsByType("com.google");
        Account account = null;
        if (acc.length>0) {
            account=acc[0];
            ContentResolver.requestSync(account, "com.android.calendar", extras);
        }

        return eventID;

    }

    @Override
    protected  Object doInBackground(Object[] objects)
    {
        try {
            int personnel_id = -100;
            SharedPreferences settings = getSharedPreferences("UserInfo", 0);
            if (settings.contains("User_ID")) {
                personnel_id = Integer.parseInt(settings.getString("User_ID", "").toString());
            }
            if (personnel_id != -100 && Integer.parseInt(settings.getString("Calender ID", "0")) > 0) {
                SOAPClass soapc = new SOAPClass(getApplicationContext());
                SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                ArrayList eventsList = soapc.getDairyEntries(personnel_id);
                if (eventsList.size() > 2) {
                    long eventID = 0;
                    for (int i = 0; i < eventsList.size(); i=i+6)
                    {

                            Date sdate = format.parse(eventsList.get(i).toString().replace("anyType{}",""));
                            Date edate = format.parse(eventsList.get(i + 1).toString().replace("anyType{}",""));

                        Calendar beginTime = Calendar.getInstance();
                        beginTime.set(sdate.getYear(), sdate.getMonth(), sdate.getDay(), sdate.getHours(), sdate.getMinutes());
                        long smillies = beginTime.getTimeInMillis();
                        Calendar endTime = Calendar.getInstance();
                        endTime.set(edate.getYear(), edate.getMonth(), edate.getDay(), edate.getHours(), edate.getMinutes());
                        long emillies = endTime.getTimeInMillis();

                       eventID = pushEventToCalender(Integer.parseInt(settings.getString("Calender ID", "0")), eventsList.get(i + 2).toString().replace("anyType{}",""), eventsList.get(i + 3).toString().replace("anyType{}",""), eventsList.get(i + 4).toString().replace("anyType{}",""), 0, smillies, emillies, 30, Boolean.FALSE,Boolean.FALSE);

                       // eventStartTimes[i] = eventsList.get(i).toString();
                       // eventEndTimes[i] = eventsList.get(i + 1).toString();
                       // eventLines[i] = eventsList.get(i + 2).toString();
                       // eventDescriptions[i] = eventsList.get(i + 3).toString();
                       // eventLocations[i] = eventsList.get(i + 4).toString();
                       // eventImage[i] = R.mipmap.ic_launcher;// "@mipmap/ic_launcher";
                    }

                    Uri shown = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);

                    Intent intent = new Intent(Intent.ACTION_VIEW).setData(shown);
                    startActivity(intent);

                    makeToastnUIThread( "Finished Importing Events To Calender " +settings.getString("Calender ID", "0"));
                }
                else {
                    makeToastnUIThread( "No Events To Add.");
                }

            }else{
                makeToastnUIThread( "No Calender Selected In Settings.");
            }
        }
        catch (Exception e){
            makeToastnUIThread(  e.getMessage());
        }

        return  null;
    }
}

另外,当它试图打开最后一个插入的事件时,它表示找不到条目。有什么想法吗?

0tdrvxhp

0tdrvxhp1#

看来我在瓦拉贝的观察下找到了答案。我的约会安排不正确。
应该是这样的:

Calendar beginTime = Calendar.getInstance();
                        beginTime.set(sdate.getYear() + 1900, sdate.getMonth(), sdate.getDate(), sdate.getHours(), sdate.getMinutes());

                        long smillies = beginTime.getTimeInMillis();
                        Calendar endTime = Calendar.getInstance();
                        endTime.set(edate.getYear() + 1900, edate.getMonth(), edate.getDate(), edate.getHours(), edate.getMinutes());
                        long emillies = endTime.getTimeInMillis();

相关问题