如何向Android棉花糖日历添加多个事件? [英] How to add multiple event to android marshmallow Calendar?

查看:65
本文介绍了如何向Android棉花糖日历添加多个事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在api 22之前,事件已正确添加到日历中.

Events are adding to calendar properly till api 22.

我还为棉花糖实现了运行时权限,可以在我的应用程序的电话"设置中允许日历日历权限.

I have also implemented run- time permissions for Marshmallow , Calender permission is allowed in Phone setting for my application is clearly visible.

但是手机日历上仍然没有任何更新,而且应用程序也没有错误或警告.

But still nothing is updating on phone calendar and also app giving no error or warning.

下面是我在电话日历上以编程方式添加事件的方法.

Below is my method to add event programatically on phone calendar.

    private void addEventToCalender(Activity ourActivity, String title, String desc, String place, int status, long startDate, long endDte, boolean needReminder, boolean needMailService) {
        try {
            String eventUriString = "content://com.android.calendar/events";
            ContentValues eventValues = new ContentValues();
            eventValues.put("calendar_id", 1); // id, We need to choose from         // our mobile for primary its 1
            eventValues.put("title", "My Title");
            eventValues.put("description","My Description" );
            eventValues.put("eventLocation", "Noida,UP ";

            eventValues.put("dtstart", startDate);
            eventValues.put("dtend", endDte);
            eventValues.put("allDay", 1); // 1 for whole day 
            //eventValues.put("rrule", "FREQ=YEARLY");

            // values.put("allDay", 1); //If it is bithday alarm or such
            // kind (which should remind me for whole day) 0 for false, 1
            // for true
            eventValues.put("eventStatus", 1); // This information is
            // sufficient for most
            // entries tentative (0),
            // confirmed (1) or canceled
            // (2):
            eventValues.put("eventTimezone", "UTC/GMT " + Constants.tzone);
            eventValues.put("hasAlarm", 1); // 0 for false, 1 for true
            Uri eventUri = this.getApplicationContext().getContentResolver().insert(Uri.parse(eventUriString), eventValues);
            long eventID = Long.parseLong(eventUri.getLastPathSegment());
            Log.i("eventID", eventID + "");
            showSnackBar("Event added to calender successfuly.");
        } catch (Exception ex) {
            Log.e("error", "Error in adding event on calendar" + ex.getMessage());
            showSnackBar("Ünable to add event to calender!");
        }

    }

推荐答案

可能是您想在Fragment内使用.

May be you want to use inside Fragment .

首次使用:

super.requestPermissions( new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR);

在片段内部,您需要调用:

Inside fragment, you need to call:

FragmentCompat.requestPermissions(permissionsList, RequestCode)

注意:

ActivityCompat.requestPermissions(Activity, permissionsList, RequestCode);

在app.gradle中为FragmentCompat类添加此库.

add this library for FragmentCompat class in app.gradle.

compile 'com.android.support:support-v13:version_of_library'

这篇关于如何向Android棉花糖日历添加多个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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