ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/util/PreferenceUtil.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.util; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import android.content.Context; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import android.content.SharedPreferences; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import android.content.SharedPreferences.Editor; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import android.os.Build; |
| TMBOY | 45:2aa9f933c8d2 | 22 | |
| TMBOY | 45:2aa9f933c8d2 | 23 | import java.util.Map; |
| TMBOY | 45:2aa9f933c8d2 | 24 | |
| TMBOY | 45:2aa9f933c8d2 | 25 | /** |
| TMBOY | 45:2aa9f933c8d2 | 26 | * 提供统一的Preference util方法 |
| TMBOY | 45:2aa9f933c8d2 | 27 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 28 | * Created by zhangyan42@baidu.com on 2017/5/24. |
| TMBOY | 45:2aa9f933c8d2 | 29 | */ |
| TMBOY | 45:2aa9f933c8d2 | 30 | public class PreferenceUtil { |
| TMBOY | 45:2aa9f933c8d2 | 31 | private static final String APP_SHARD = "com.baidu.duer.dcs"; |
| TMBOY | 45:2aa9f933c8d2 | 32 | |
| TMBOY | 45:2aa9f933c8d2 | 33 | /** |
| TMBOY | 45:2aa9f933c8d2 | 34 | * 保存数据的方法,拿到保存数据的具体类型,然后根据类型调用不同的保存方法 |
| TMBOY | 45:2aa9f933c8d2 | 35 | * |
| TMBOY | 45:2aa9f933c8d2 | 36 | * @param context 上下文 |
| TMBOY | 45:2aa9f933c8d2 | 37 | * @param key key |
| TMBOY | 45:2aa9f933c8d2 | 38 | * @param object value |
| TMBOY | 45:2aa9f933c8d2 | 39 | */ |
| TMBOY | 45:2aa9f933c8d2 | 40 | public static void put(Context context, String key, Object object) { |
| TMBOY | 45:2aa9f933c8d2 | 41 | put(context, APP_SHARD, key, object); |
| TMBOY | 45:2aa9f933c8d2 | 42 | } |
| TMBOY | 45:2aa9f933c8d2 | 43 | |
| TMBOY | 45:2aa9f933c8d2 | 44 | public static void put(Context context, String spName, String key, Object object) { |
| TMBOY | 45:2aa9f933c8d2 | 45 | SharedPreferences sp = context.getSharedPreferences(spName, |
| TMBOY | 45:2aa9f933c8d2 | 46 | Context.MODE_PRIVATE); |
| TMBOY | 45:2aa9f933c8d2 | 47 | Editor editor = sp.edit(); |
| TMBOY | 45:2aa9f933c8d2 | 48 | |
| TMBOY | 45:2aa9f933c8d2 | 49 | if (object instanceof String) { |
| TMBOY | 45:2aa9f933c8d2 | 50 | editor.putString(key, (String) object); |
| TMBOY | 45:2aa9f933c8d2 | 51 | } else if (object instanceof Integer) { |
| TMBOY | 45:2aa9f933c8d2 | 52 | editor.putInt(key, (Integer) object); |
| TMBOY | 45:2aa9f933c8d2 | 53 | } else if (object instanceof Boolean) { |
| TMBOY | 45:2aa9f933c8d2 | 54 | editor.putBoolean(key, (Boolean) object); |
| TMBOY | 45:2aa9f933c8d2 | 55 | } else if (object instanceof Float) { |
| TMBOY | 45:2aa9f933c8d2 | 56 | editor.putFloat(key, (Float) object); |
| TMBOY | 45:2aa9f933c8d2 | 57 | } else if (object instanceof Long) { |
| TMBOY | 45:2aa9f933c8d2 | 58 | editor.putLong(key, (Long) object); |
| TMBOY | 45:2aa9f933c8d2 | 59 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 60 | editor.putString(key, object.toString()); |
| TMBOY | 45:2aa9f933c8d2 | 61 | } |
| TMBOY | 45:2aa9f933c8d2 | 62 | editSubmit(editor); |
| TMBOY | 45:2aa9f933c8d2 | 63 | } |
| TMBOY | 45:2aa9f933c8d2 | 64 | |
| TMBOY | 45:2aa9f933c8d2 | 65 | private static void editSubmit(Editor editor) { |
| TMBOY | 45:2aa9f933c8d2 | 66 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { |
| TMBOY | 45:2aa9f933c8d2 | 67 | editor.apply(); |
| TMBOY | 45:2aa9f933c8d2 | 68 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 69 | editor.commit(); |
| TMBOY | 45:2aa9f933c8d2 | 70 | } |
| TMBOY | 45:2aa9f933c8d2 | 71 | } |
| TMBOY | 45:2aa9f933c8d2 | 72 | |
| TMBOY | 45:2aa9f933c8d2 | 73 | /** |
| TMBOY | 45:2aa9f933c8d2 | 74 | * 得到保存数据的方法, |
| TMBOY | 45:2aa9f933c8d2 | 75 | * 根据默认值得到保存的数据的具体类型, |
| TMBOY | 45:2aa9f933c8d2 | 76 | * 然后调用相对于的方法获取值 |
| TMBOY | 45:2aa9f933c8d2 | 77 | * |
| TMBOY | 45:2aa9f933c8d2 | 78 | * @param context 上下文 |
| TMBOY | 45:2aa9f933c8d2 | 79 | * @param key key |
| TMBOY | 45:2aa9f933c8d2 | 80 | * @param defaultObject default-value |
| TMBOY | 45:2aa9f933c8d2 | 81 | */ |
| TMBOY | 45:2aa9f933c8d2 | 82 | public static Object get(Context context, String key, Object defaultObject) { |
| TMBOY | 45:2aa9f933c8d2 | 83 | return get(context, APP_SHARD, key, defaultObject); |
| TMBOY | 45:2aa9f933c8d2 | 84 | } |
| TMBOY | 45:2aa9f933c8d2 | 85 | |
| TMBOY | 45:2aa9f933c8d2 | 86 | public static Object get(Context context, String spName, String key, Object defaultObject) { |
| TMBOY | 45:2aa9f933c8d2 | 87 | SharedPreferences sp = context.getSharedPreferences(spName, |
| TMBOY | 45:2aa9f933c8d2 | 88 | Context.MODE_PRIVATE); |
| TMBOY | 45:2aa9f933c8d2 | 89 | |
| TMBOY | 45:2aa9f933c8d2 | 90 | if (defaultObject instanceof String) { |
| TMBOY | 45:2aa9f933c8d2 | 91 | return sp.getString(key, (String) defaultObject); |
| TMBOY | 45:2aa9f933c8d2 | 92 | } else if (defaultObject instanceof Integer) { |
| TMBOY | 45:2aa9f933c8d2 | 93 | return sp.getInt(key, (Integer) defaultObject); |
| TMBOY | 45:2aa9f933c8d2 | 94 | } else if (defaultObject instanceof Boolean) { |
| TMBOY | 45:2aa9f933c8d2 | 95 | return sp.getBoolean(key, (Boolean) defaultObject); |
| TMBOY | 45:2aa9f933c8d2 | 96 | } else if (defaultObject instanceof Float) { |
| TMBOY | 45:2aa9f933c8d2 | 97 | return sp.getFloat(key, (Float) defaultObject); |
| TMBOY | 45:2aa9f933c8d2 | 98 | } else if (defaultObject instanceof Long) { |
| TMBOY | 45:2aa9f933c8d2 | 99 | return sp.getLong(key, (Long) defaultObject); |
| TMBOY | 45:2aa9f933c8d2 | 100 | } |
| TMBOY | 45:2aa9f933c8d2 | 101 | return null; |
| TMBOY | 45:2aa9f933c8d2 | 102 | } |
| TMBOY | 45:2aa9f933c8d2 | 103 | |
| TMBOY | 45:2aa9f933c8d2 | 104 | /** |
| TMBOY | 45:2aa9f933c8d2 | 105 | * 移除某个key值已经对应的值 |
| TMBOY | 45:2aa9f933c8d2 | 106 | * |
| TMBOY | 45:2aa9f933c8d2 | 107 | * @param context 上下文 |
| TMBOY | 45:2aa9f933c8d2 | 108 | * @param key key |
| TMBOY | 45:2aa9f933c8d2 | 109 | */ |
| TMBOY | 45:2aa9f933c8d2 | 110 | public static void remove(Context context, String key) { |
| TMBOY | 45:2aa9f933c8d2 | 111 | remove(context, APP_SHARD, key); |
| TMBOY | 45:2aa9f933c8d2 | 112 | } |
| TMBOY | 45:2aa9f933c8d2 | 113 | |
| TMBOY | 45:2aa9f933c8d2 | 114 | public static void remove(Context context, String spName, String key) { |
| TMBOY | 45:2aa9f933c8d2 | 115 | SharedPreferences sp = context.getSharedPreferences(spName, |
| TMBOY | 45:2aa9f933c8d2 | 116 | Context.MODE_PRIVATE); |
| TMBOY | 45:2aa9f933c8d2 | 117 | Editor editor = sp.edit(); |
| TMBOY | 45:2aa9f933c8d2 | 118 | editor.remove(key); |
| TMBOY | 45:2aa9f933c8d2 | 119 | editSubmit(editor); |
| TMBOY | 45:2aa9f933c8d2 | 120 | } |
| TMBOY | 45:2aa9f933c8d2 | 121 | |
| TMBOY | 45:2aa9f933c8d2 | 122 | /** |
| TMBOY | 45:2aa9f933c8d2 | 123 | * 清除所有数据 |
| TMBOY | 45:2aa9f933c8d2 | 124 | * |
| TMBOY | 45:2aa9f933c8d2 | 125 | * @param context 上下文 |
| TMBOY | 45:2aa9f933c8d2 | 126 | */ |
| TMBOY | 45:2aa9f933c8d2 | 127 | public static void clear(Context context) { |
| TMBOY | 45:2aa9f933c8d2 | 128 | clear(context, APP_SHARD); |
| TMBOY | 45:2aa9f933c8d2 | 129 | } |
| TMBOY | 45:2aa9f933c8d2 | 130 | |
| TMBOY | 45:2aa9f933c8d2 | 131 | public static void clear(Context context, String spName) { |
| TMBOY | 45:2aa9f933c8d2 | 132 | SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE); |
| TMBOY | 45:2aa9f933c8d2 | 133 | Editor editor = sp.edit(); |
| TMBOY | 45:2aa9f933c8d2 | 134 | editor.clear(); |
| TMBOY | 45:2aa9f933c8d2 | 135 | editSubmit(editor); |
| TMBOY | 45:2aa9f933c8d2 | 136 | } |
| TMBOY | 45:2aa9f933c8d2 | 137 | |
| TMBOY | 45:2aa9f933c8d2 | 138 | /** |
| TMBOY | 45:2aa9f933c8d2 | 139 | * 查询某个key是否已经存在 |
| TMBOY | 45:2aa9f933c8d2 | 140 | * |
| TMBOY | 45:2aa9f933c8d2 | 141 | * @param context 上下文 |
| TMBOY | 45:2aa9f933c8d2 | 142 | * @param key key |
| TMBOY | 45:2aa9f933c8d2 | 143 | */ |
| TMBOY | 45:2aa9f933c8d2 | 144 | public static boolean contains(Context context, String key) { |
| TMBOY | 45:2aa9f933c8d2 | 145 | return contains(context, APP_SHARD, key); |
| TMBOY | 45:2aa9f933c8d2 | 146 | } |
| TMBOY | 45:2aa9f933c8d2 | 147 | |
| TMBOY | 45:2aa9f933c8d2 | 148 | public static boolean contains(Context context, String spName, String key) { |
| TMBOY | 45:2aa9f933c8d2 | 149 | SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE); |
| TMBOY | 45:2aa9f933c8d2 | 150 | return sp.contains(key); |
| TMBOY | 45:2aa9f933c8d2 | 151 | } |
| TMBOY | 45:2aa9f933c8d2 | 152 | |
| TMBOY | 45:2aa9f933c8d2 | 153 | /** |
| TMBOY | 45:2aa9f933c8d2 | 154 | * 返回所有的键值对 |
| TMBOY | 45:2aa9f933c8d2 | 155 | * |
| TMBOY | 45:2aa9f933c8d2 | 156 | * @param context 上下文 |
| TMBOY | 45:2aa9f933c8d2 | 157 | */ |
| TMBOY | 45:2aa9f933c8d2 | 158 | public static Map<String, ?> getAll(Context context) { |
| TMBOY | 45:2aa9f933c8d2 | 159 | return getAll(context, APP_SHARD); |
| TMBOY | 45:2aa9f933c8d2 | 160 | } |
| TMBOY | 45:2aa9f933c8d2 | 161 | |
| TMBOY | 45:2aa9f933c8d2 | 162 | public static Map<String, ?> getAll(Context context, String spName) { |
| TMBOY | 45:2aa9f933c8d2 | 163 | SharedPreferences sp = context.getSharedPreferences(spName, |
| TMBOY | 45:2aa9f933c8d2 | 164 | Context.MODE_PRIVATE); |
| TMBOY | 45:2aa9f933c8d2 | 165 | return sp.getAll(); |
| TMBOY | 45:2aa9f933c8d2 | 166 | } |
| TMBOY | 45:2aa9f933c8d2 | 167 | } |
