通过日历提供程序添加的事件未显示在Android日历应用程序上

问题描述 投票:1回答:4

我正在尝试将事件添加到默认的Android日历,而不要求用户确认保存事件。 (所以没有意图)

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

我已经检查了清单中的日历授权,并在graddle中查看de min sdk版本(我在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 ?");
    }
}
android calendar google-calendar-api android-calendar
4个回答
1
投票

默认日历ID为3,但您有0,这可能是您没有看到添加到日历的原因。

Calendar Provider developer doc,它将日历ID硬编码为3。

long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
...

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);

// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());

要确保3是主日历ID,您可以参考此post查询主要/默认日历


1
投票

这将向用户日历添加事件。

添加清单的权限:

 <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" />

添加到oncreate:

if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) !=          PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, 1);

添加到mainactivity :(查看视图)(MenuItem mi)用于菜单抽屉。

public void onAddEventCal(MenuItem mi) {

    long calID = 3;
    ContentResolver cr = getContentResolver();
    Calendar beginTime = Calendar.getInstance();
    beginTime.set(2019, Calendar.FEBRUARY, 25, 3, 30);
    Calendar endTime = Calendar.getInstance();
    endTime.set(2019, Calendar.FEBRUARY, 25, 3, 30);
    ContentValues values = new ContentValues();
    values.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis());
    values.put(CalendarContract.Events.DTEND, endTime.getTimeInMillis());
    values.put(CalendarContract.Events.TITLE, "Connect Treadmill app");
    values.put(CalendarContract.Events.DESCRIPTION, "Treadmill Data");
    values.put(CalendarContract.Events.CALENDAR_ID, calID);
    values.put(CalendarContract.Events.EVENT_TIMEZONE, "Europe/Paris");
    values.put(CalendarContract.Events.EVENT_LOCATION, "Paris");
    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);


    Toast.makeText(this, "Calendar Event added", Toast.LENGTH_SHORT).show();

}

0
投票

我认为问题在于允许尝试使用这些代码

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},MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);}else if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED 
        && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED){



}

0
投票

这个权限请求就是我使用的。您可以修改所需的权限。

 onCreate

 // Ask for location && calendar permission if not already allowed
    Request_Permissions();




ManActivity
//Request permission
private void Request_Permissions()
{
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR, android.Manifest.permission.ACCESS_COARSE_LOCATION
    }, 1);
}

0
投票

我遇到了这个问题,然后我使用以下代码来获取日历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传递。请改用上述功能。

© www.soinside.com 2019 - 2024. All rights reserved.