SunshinePreferences.java 文件源码

java
阅读 33 收藏 0 点赞 0 评论 0

项目:android-dev-challenge 作者:
/**
 * Returns true if the user prefers to see notifications from Sunshine, false otherwise. This
 * preference can be changed by the user within the SettingsFragment.
 *
 * @param context Used to access SharedPreferences
 * @return true if the user prefers to see notifications, false otherwise
 */
public static boolean areNotificationsEnabled(Context context) {
    /* Key for accessing the preference for showing notifications */
    String displayNotificationsKey = context.getString(R.string.pref_enable_notifications_key);

    /*
     * In Sunshine, the user has the ability to say whether she would like notifications
     * enabled or not. If no preference has been chosen, we want to be able to determine
     * whether or not to show them. To do this, we reference a bool stored in bools.xml.
     */
    boolean shouldDisplayNotificationsByDefault = context
            .getResources()
            .getBoolean(R.bool.show_notifications_by_default);

    /* As usual, we use the default SharedPreferences to access the user's preferences */
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

    /* If a value is stored with the key, we extract it here. If not, use a default. */
    boolean shouldDisplayNotifications = sp
            .getBoolean(displayNotificationsKey, shouldDisplayNotificationsByDefault);

    return shouldDisplayNotifications;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号