简体   繁体   English

在android中,如何在安装应用程序时创建共享首选项?

[英]In android how to create a shared preferences when the application is installed?

Hi I'am new to android. 嗨,我是android新手。
I want to create a shared preferences when the app is first installed and the insert some data. 我想在首次安装应用程序并插入一些数据时创建共享首选项。
The shared preference has to be used from all activities in the app. 必须在应用程序的所有活动中使用共享的首选项。
I tried creating the shared preference in the onCreate() function of the first activity and inserted values into it.And edited the data from another activity. 我尝试在第一个活动的onCreate()函数中创建共享首选项并将值插入其中,然后编辑另一个活动中的数据。
But when I restart the app the shared preference changes to the data give in the onCreate(). 但是,当我重新启动应用程序时,共享首选项更改为onCreate()中提供的数据。
Can somebody help me? 有人可以帮我吗?

The way if you want to insert data once: 如果要一次插入数据的方式:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    SharedPreferences preferences = getSharedPreferences("myPref", MODE_PRIVATE);
    boolean shouldInsertData = preferences.getBoolean("shouldInsertData", true);

    if(shouldInsertData){

        //insert your data into the preferences

        preferences.edit().putBoolean("shouldInsertData", false).apply();

    }

}

Share Preference is best way to store short information. 共享首选项是存储简短信息的最佳方法。 but if you will create share preference from an active, it may create problem to access it from other activity. 但是如果您要从一个活动中创建共享首选项,则可能会出现从其他活动访问它的问题。 You should create a global common share preference from application so you can access it through out the android project, any where in any activity. 您应该从应用程序创建一个全局通用共享首选项,以便您可以在android项目中的任何活动中的任何位置访问它。 I get reference from here - It is 4 step process. 从这里获取参考 -这是4个步骤的过程。

Step 1 - create a java class file name as "myapp" and extend it by application. 第1步 -创建一个名为“ myapp”的Java类文件,并通过应用程序对其进行扩展。


public class MyApp extends Application {
private static Context context;
private String TAG ="myApp";
@Override
public void onCreate() {
    super.onCreate();
    context = getApplicationContext();
   // Log.e(TAG, "  myapp stater");
}

public static Context getContext(){
    return context;
}}

Step 2 - in android manifest file inside application tab add android:name=".myapp" 第2步 -在“应用程序”标签内的android清单文件中添加android:name =“。myapp”

Step 3 - Create java class name as "SharePreferenceUtils" (note don't use name SharePreference.) 步骤3-将 Java类名称创建为“ SharePreferenceUtils”(注意不要使用名称SharePreference。)


public class SharePreferenceUtils { 公共类SharePreferenceUtils {

private static String PREFERENCE_NAME = "shopeasy-ecommerce";
private static SharePreferenceUtils sharePreferenceUtils;
private SharedPreferences sharedPreferences;

private SharePreferenceUtils(Context context){
    PREFERENCE_NAME = PREFERENCE_NAME + context.getPackageName();
    this.sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}

public static SharePreferenceUtils getInstance(){
    if (sharePreferenceUtils == null){
         sharePreferenceUtils = new SharePreferenceUtils(SplashActivity.getContext());
    }
    return sharePreferenceUtils;
}

// login response user_id 1234
public void saveString(String key,  String Val ){
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, Val);
    editor.commit();
}

public String getString(String key, String defVal){
    return sharedPreferences.getString(key, defVal);
}


public String getString(String key){
    return sharedPreferences.getString(key, "");
}

public void saveInt(String key,  int Val ){
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, Val);
    editor.commit();
}

public int getInteger(String key){ return sharedPreferences.getInt(key, 0 ); }

/**
 * Clear all values from this preference
 */
public void clear() {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    editor.commit();
}

/**
 * Clear value of given key from this preference
 *
 * @param key name of the key whose value to be removed
 */
public void clearString(String key) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.remove(key);
    editor.commit();
}}

Step 4 Call sharePreferenceUtil from activity SharePreferenceUtils.getInstance().saveString("Username", "username value here"); 步骤4从活动SharePreferenceUtils.getInstance()。saveString(“ Username”,“此处的用户名值”)调用sharePreferenceUtil

Now you can access sharepreference from any activity. 现在,您可以从任何活动访问sharepreference。 Just call instance of sharepreference java class. 只需调用sharepreference java类的实例即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM