通过Calendar Provider添加的事件未显示在Android Calendar App上 [英] Event added through Calendar Provider is not showing up on the Android Calendar App

查看:286
本文介绍了通过Calendar Provider添加的事件未显示在Android Calendar App上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将事件添加到默认的android日历中,而不要求用户确认保存事件. (所以不是故意的)

I'm trying to add event to default android calendar whithout asking the user for a confirmation of saving the event. (so not with intent)

以下代码没有编译错误或运行时错误.单击按钮后,不会显示任何错误,也不会向Android日历应用添加事件.

The following code has no compilation error or run time error. After clicking on the button, no error is shown and no event is added to the Android Calendar App.

我已经检查了清单中的日历授权,并在gradle中检查了de min sdk版本(我在android studio上工作)

I've already check the calendars authorisations in my manifest and check de min sdk version in the graddle (i work on android studio)

public void onAddEventClicked(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }
    else if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
        ContentResolver cr = getContentResolver();
        Calendar beginTime = Calendar.getInstance();
        beginTime.set(2018, Calendar.OCTOBER, 27, 13, 30);
        Calendar endTime = Calendar.getInstance();
        endTime.set(2018, Calendar.OCTOBER, 27, 17, 30);
        ContentValues values = new ContentValues();
        values.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis());
        values.put(CalendarContract.Events.DTEND, endTime.getTimeInMillis());
        values.put(CalendarContract.Events.TITLE, "doctor");
        values.put(CalendarContract.Events.DESCRIPTION, "doctor appointment");
        values.put(CalendarContract.Events.CALENDAR_ID, 0);
        values.put(CalendarContract.Events.EVENT_TIMEZONE, "Europe/Paris");
        values.put(CalendarContract.Events.EVENT_LOCATION, "Paris");
        Uri EVENTS_URI = Uri.parse(CalendarContract.Events.CONTENT_URI.toString());
        Uri uri = cr.insert(EVENTS_URI, values);
        Button b = (Button) findViewById(R.id.addEventButton);
        b.setText("Did it worked ?");
    }
}

推荐答案

我遇到了这个问题,然后使用以下代码获取日历ID.

I was getting this issue and then I used following code to get the calendar ID.

private fun getCalendarId(context: Context) : Long? {
    val projection = arrayOf(Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME)

    var calCursor = context.contentResolver.query(
        Calendars.CONTENT_URI,
        projection,
        Calendars.VISIBLE + " = 1 AND " + Calendars.IS_PRIMARY + "=1",
        null,
        Calendars._ID + " ASC"
    )

    if (calCursor != null && calCursor.count >= 0) {
        calCursor = context.contentResolver.query(
            Calendars.CONTENT_URI,
            projection,
            Calendars.VISIBLE + " = 1",
            null,
            Calendars._ID + " ASC"
        )
    }

    if (calCursor != null) {
        if (calCursor.moveToFirst()) {
            val calName: String
            val calID: String
            val nameCol = calCursor.getColumnIndex(projection[1])
            val idCol = calCursor.getColumnIndex(projection[0])

            calName = calCursor.getString(nameCol)
            calID = calCursor.getString(idCol)

            Log.d("Calendar name = $calName Calendar ID = $calID")

            calCursor.close()
            return calID.toLong()
        }
    }
    return null
}

因此,请勿将0、1或3作为日历ID .请改用上述功能.

So do not pass 0, 1 or 3 as Calendar ID. Use above function instead.

这篇关于通过Calendar Provider添加的事件未显示在Android Calendar App上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆