0

不幸的是,我找不到任何关于此的帮助。

实际上我想从移动设备获取所有可用的日历(可能是日历偏好)列表,例如谷歌日历、雅虎日历。

为了更好地解释,我在从小部件单击配置按钮后从Smooth Calendar应用程序中捕获了一些图像,这些图像位于下图中。

在此处输入图像描述 ==> 在此处输入图像描述

此处日历首选项显示手机中可用的所有日历,在选择日历选项后,它显示所有日历以选择用户想要的内容。

有人可以在这里帮助我并分享一些如何做到这一点的知识。

谢谢

4

2 回答 2

1

要从移动设备获取所有可用的日历,希望此代码会有所帮助。

Cursor cursor;

        if (android.os.Build.VERSION.SDK_INT <= 7) {
            cursor = getContentResolver().query(Uri.parse("content://calendar/calendars"), new String[] { "_id", "displayName" }, null,
                    null, null);

        }

        else if (android.os.Build.VERSION.SDK_INT <= 14) {
            cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),
                    new String[] { "_id", "displayName" }, null, null, null);

        }

        else {
            cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),
                    new String[] { "_id", "calendar_displayName" }, null, null, null);

        }

        // Get calendars name
        Log.i("@calendar","Cursor count " + cursor.getCount());
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            String[] calendarNames = new String[cursor.getCount()];
            // Get calendars id
            int calendarIds[] = new int[cursor.getCount()];
            for (int i = 0; i < cursor.getCount(); i++) {
                calendarIds[i] = cursor.getInt(0);
                calendarNames[i] = cursor.getString(1);
                Log.i("@calendar","Calendar Name : " + calendarNames[i]);
                cursor.moveToNext();
            }
        } else {
            Log.e("@calendar","No calendar found in the device");
        }
于 2018-08-13T10:23:26.703 回答
0

幸运的是,我从开发者网站找到了一些帮助,并且能够获取所有可用的日历并使用动态 Checkpreferences 显示它们。

希望我的代码将来能对某些人有所帮助。

CalendarPreference.java

public class CalendarPreference extends PreferenceActivity{
private static final String CALENDAR_ID = "calendarId";
private static final String[] PROJECTION = new String[] { Calendars._ID,
        Calendars.CALENDAR_DISPLAY_NAME, Calendars.CALENDAR_COLOR };
private Set<String> initialActiveCalendars;

CheckBoxPreference mCheckBoxPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.calendaraccounts);
    SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
    initialActiveCalendars = prefs.getStringSet("PREF_ACTIVE_CALENDARS", null);
    populatePreferenceScreen(initialActiveCalendars);


}

private void populatePreferenceScreen(Set<String> activeCalendars) {
    Cursor cursor = createLoadedCursor();
    if (cursor == null) {
        return;
    }
    for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToPosition(i);
        CheckBoxPreference checkboxPref = new CheckBoxPreference(this);
        checkboxPref.setTitle(cursor.getString(1));
        checkboxPref.setIcon(createDrawable(cursor.getInt(2)));
        int calendarId = cursor.getInt(0);
        checkboxPref.getExtras().putInt(CALENDAR_ID, calendarId);
        checkboxPref.setChecked(activeCalendars == null
                || activeCalendars.contains(String.valueOf(calendarId)));
        getPreferenceScreen().addPreference(checkboxPref);
    }
}

    @Override
       public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        return true;
    }


    private Cursor createLoadedCursor() {
    Uri.Builder builder = Calendars.CONTENT_URI.buildUpon();
    ContentResolver contentResolver = getContentResolver();
    return contentResolver.query(builder.build(), PROJECTION, null, null, null);
   }

    @Override
    public void onPause() {
        super.onPause();        
        HashSet<String> selectedCalendars = getSelectedCalenders();  
        if (!selectedCalendars.equals(initialActiveCalendars)) {
            persistSelectedCalendars(selectedCalendars);
            Log.v("Selected Calendars", selectedCalendars.toString());
            NewWidget.updateAllWidgets(this);
        }
    }

    private void persistSelectedCalendars(HashSet<String> prefValues) {
        SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
        Editor editor = prefs.edit();
        editor.putStringSet("PREF_ACTIVE_CALENDARS", prefValues);
        editor.commit();
    }

    private HashSet<String> getSelectedCalenders() {
        PreferenceScreen preferenceScreen = getPreferenceScreen();
        int prefCount = preferenceScreen.getPreferenceCount();
        HashSet<String> prefValues = new HashSet<String>();
        for (int i = 0; i < prefCount; i++) {
            Preference pref = preferenceScreen.getPreference(i);
            if (pref instanceof CheckBoxPreference) {
                CheckBoxPreference checkPref = (CheckBoxPreference) pref;
                if (checkPref.isChecked()) {
                    prefValues.add(String.valueOf(checkPref.getExtras().getInt(CALENDAR_ID)));
                }
            }
        }
        return prefValues;
    }

    private Drawable createDrawable(int color) {
        Drawable drawable = getResources().getDrawable(R.drawable.prefs_calendar_entry);
        drawable.setColorFilter(new LightingColorFilter(0x0, color));
        return drawable;
    }


}

这里是 res/xml/calendaraccounts.xml

<PreferenceScreen>

</PreferenceScreen>
于 2014-04-28T10:19:05.117 回答