Android:如何从移动设备获取所有可用的日历

时间:2014-03-29 10:05:20

标签: android calendar android-widget checkboxpreference

很遗憾,我无法找到任何帮助。

其实我想从手机上获取所有可用的日历(可能是日历首选项)列表,例如Google日历,雅虎日历。

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

enter image description here ==> enter image description here

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

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

由于

2 个答案:

答案 0 :(得分: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");
        }

答案 1 :(得分: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>