将事件添加到Android中的日历时,CALENDAR_ID应该是什么? [英] What should be CALENDAR_ID while adding event to calendar in android?

查看:345
本文介绍了将事件添加到Android中的日历时,CALENDAR_ID应该是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我想知道"CalendarContract.Events.CALENDAR_ID"的值是什么?这段代码适用于Android N之前的操作系统,但是在牛轧糖中,如果我输入"1"作为值,它将在特定字段(提醒,生日,假日等)中创建一个事件,并且会因用户而异.因此,如果我在某些牛轧糖设备中将"5"作为值传递,那么我可能会在一台设备中获得正确的输出,但在另一部手机中,该事件可能是通过提醒或假期创建的,并在几秒钟后自动删除.

Here is my Code and I want to know what is value for "CalendarContract.Events.CALENDAR_ID"? This code is working on Pre Android N Os but in Nougat if I put "1" as value it creates an event in specific Field(Reminder, Birthday, Holiday etc) and it varies user to user. So if I pass "5" as value in some nougat device then I might get proper output in one device but in another phone that event might gets created with reminder or holiday and gets deleted automatically with in few seconds.

        ContentResolver cr = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put(CalendarContract.Events.DTSTART, startMillis);
        values.put(CalendarContract.Events.DTEND, endMillis);
        values.put(CalendarContract.Events.TITLE, title);
        values.put(CalendarContract.Events.CALENDAR_ID, 1);
        values.put(CalendarContract.Events.EVENT_TIMEZONE, 
        Calendar.getInstance().getTimeZone().getID()); 
        Uri uri=cr.insert(CalendarContract.Events.CONTENT_URI,
        values);
        long eventId = Long.parseLong(uri.getLastPathSegment());

推荐答案

ID取决于设备制造商添加的日历数量.但是您只能使用用户日历.例如,如果您将活动添加到假期日历中,则会在同步时将其删除.用户还可以将几个Google用户添加到手机中,因此在这种情况下,更好的选择是询问用户他要使用哪个日历:

ID depends on quantity of calendars added by device manufacturer. But you can use only user calendar. If you add event, for example, to the holidays calendar it will be removed on sync. Also user can have several Google users added to phone, so in this case the better option is to ask user what calendar he want to use:

 public class CalendarItem{
    private int id;
    private String name;

    ... getters and setters ...
}

public List<CalendarItem> getCalendars(Context ctx){
    if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        return null;
    }

    List<CalendarItem> result = new ArrayList();

    final String[] EVENT_PROJECTION = new String[]{
            CalendarContract.Calendars._ID,
            CalendarContract.Calendars.NAME
    };

    final ContentResolver cr = ctx.getContentResolver();
    final Uri uri = CalendarContract.Calendars.CONTENT_URI;
    Cursor cur = cr.query(uri, EVENT_PROJECTION, null, null, null);

    while (cur.moveToNext()) {
        CalendarItem item = new CalendarItem();
        item.setId(cur.getInt(cur.getColumnIndex(CalendarContract.Calendars._ID)));
        item.setName(cur.getString(cur.getColumnIndex(CalendarContract.Calendars.NAME)));
        if (isEmailValid(item.getName)){
            result.add(item);
        }
    }
    cur.close();
    return result;
}

//user calendar name == email
private boolean isEmailValid(String email) {
    if (TextUtils.isEmpty(email)) return false;
    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(email);
    return matcher.matches();
}

这篇关于将事件添加到Android中的日历时,CALENDAR_ID应该是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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