安卓(Android) 怎么获取选定(Selected)日历(Calendar)

2019年10月1日23:00:05安卓(Android) 怎么获取选定(Selected)日历(Calendar)已关闭评论 293 1310字阅读4分22秒

1.简介

安卓(Android) 怎么获取选定(Selected)日历(Calendar)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import android.content.Context;
import android.database.Cursor;
import android.provider.CalendarContract;
import android.util.Log;
import android.util.Pair;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

public class Main{
    public static Set<String> getSelectedCalendars(Context context) {

        Set<String> selectedCalendars = null;
        final List<Pair<String, Boolean>> allCalendars = getAllCalendars(context);

        
        
        selectedCalendars = new HashSet<String>();
        for (Pair<String, Boolean> pair : allCalendars) {
            if (pair.second) {
                selectedCalendars.add(pair.first);
            }
        }

        return selectedCalendars;
    }
    public static List<Pair<String, Boolean>> getAllCalendars(
            Context context) {
        
        
        List<Pair<String, Boolean>> calendars = new ArrayList<Pair<String, Boolean>>();

        try {
            Cursor cursor = context.getContentResolver().query(
                    CalendarContract.Calendars.CONTENT_URI,
                    CalendarsQuery.PROJECTION,
                    CalendarContract.Calendars.SYNC_EVENTS + "=1", null,
                    null);

            if (cursor != null) {
                while (cursor.moveToNext()) {
                    calendars.add(new Pair<String, Boolean>(cursor
                            .getString(CalendarsQuery.ID), cursor
                            .getInt(CalendarsQuery.VISIBLE) == 1));

                }

                cursor.close();
            }

        } catch (SecurityException e) {
            Log.v("launcher_calendar", e.getMessage());
            return null;
        }

        return calendars;
    }
}

 
继续阅读
编程十万个怎么办