我不知道我必須設置該變量的初始值:如果改變每次如何初始值賦給一個字符串中的Android
mySharedPreferences.putStringValue("hello", "400");
它鴕鳥政策復位我打開應用程序。
謝謝!
我不知道我必須設置該變量的初始值:如果改變每次如何初始值賦給一個字符串中的Android
mySharedPreferences.putStringValue("hello", "400");
它鴕鳥政策復位我打開應用程序。
謝謝!
做到這一點的最好辦法是「套」上get方法,而不是設置它的默認值在你的應用程序的第一次啓動。
mySharedPreferences.getString("hello", "400");
這樣android會檢查是否有值集。否則,它將回退到默認的「400」。
而且在應用標籤
<application
android:name=".YourApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
在應用程序的onCreate方法。這是最好的地方。它會在您的應用程序創建時執行。
public class YourApplication extends Application {
public static Boolean sAppOpened = false;
@Override
public void onCreate() {
super.onCreate();
//Write your code here
sAppOpened = true;
....
}
...
}
聲明你的共享偏好設置文件名類範圍的這樣的 -
public static final String SHARED_PREFERENCES = "SHARED_PREF";
您可以在OnCreate
方法使用。
SharedPreferences sharedpreferences = getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();