SunshinePreferences.java 文件源码

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

项目:android-dev-challenge 作者:
/**
 * Returns the location coordinates associated with the location. Note that there is a
 * possibility that these coordinates may not be set, which results in (0,0) being returned.
 * Interestingly, (0,0) is in the middle of the ocean off the west coast of Africa.
 *
 * @param context used to access SharedPreferences
 * @return an array containing the two coordinate values for the user's preferred location
 */
public static double[] getLocationCoordinates(Context context) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

    double[] preferredCoordinates = new double[2];

    /*
     * This is a hack we have to resort to since you can't store doubles in SharedPreferences.
     *
     * Double.doubleToLongBits returns an integer corresponding to the bits of the given
     * IEEE 754 double precision value.
     *
     * Double.longBitsToDouble does the opposite, converting a long (that represents a double)
     * into the double itself.
     */
    preferredCoordinates[0] = Double
             .longBitsToDouble(sp.getLong(PREF_COORD_LAT, Double.doubleToRawLongBits(0.0)));
    preferredCoordinates[1] = Double
            .longBitsToDouble(sp.getLong(PREF_COORD_LONG, Double.doubleToRawLongBits(0.0)));

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


问题


面经


文章

微信
公众号

扫码关注公众号