且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android中保存和读取数据的***方法

更新时间:2022-12-18 09:24:40

如果你只是想发送数据到下一个activity,你可以使用 intent.putExtra(); getIntent()。getStringExtras();



如果要保存数据,直到用户下次更改您可以使用 SharedPrefrance ,因为您有少量的数据。



如果您的数据是敏感的(想提供更多的安全性)或加密并放入数据库



,因为您正在处理用户的用户名和密码,您应该使用sqlite数据库。

$ b
$ b
  public class GreetingCardData {
public static final String SHARED_PREF_FILE =greetingCardData;
public static final String KEY_DO_NOT_SHOW =doNotShow;
public static final String KEY_CATEGORIES_JSON =categoriesJson;
private SharedPreferences sharedPrefs;
private编辑器prefsEditor;

public GreetingCardData(Context context){
this.sharedPrefs = context.getSharedPreferences(SHARED_PREF_FILE,0);
this.prefsEditor = sharedPrefs.edit();
}

public void setDoNotShowFlag(boolean flag){
prefsEditor.putBoolean(KEY_DO_NOT_SHOW,flag);
prefsEditor.commit();
}

public boolean getDoNotShowFlag(){
return sharedPrefs.getBoolean(KEY_DO_NOT_SHOW,false);
}

public void setGreetingcardJson(String jsonString){
prefsEditor.putString(KEY_CATEGORIES_JSON,jsonString);
prefsEditor.commit();
}

public String getGreetingcardJsonString(){
return sharedPrefs.getString(KEY_CATEGORIES_JSON,);
}
}


保存数据:

  new GreetingCardData(ActivityMain.this).setDoNotShowFlag(flag); 

获取数据:

  boolean flag = new GreetingCardData(ActivityMain.this).getDoNotShowFlag(); 


I have an apps that using proxy to browse the url connection. But proxy host, port, username and password that i used are using hardcode. I already achieved that. But now i want to replace that hardcode with just an object that refer to the data. so the user can input the proxy that they want and can use their username and password. here is what my flow look like.

So first user can enter the name of the connection, host, port, etc that they want. And after that "save" it. And then in next activity it will read the data of that connection and if it exist, user can just to click and browse it. and if it dont, they can create the connection. If want to create it (it will back to creating connection form).

Thanks,

if you simply want to send data to next actvity you can use intent.putExtra(); and in next activity call getIntent().getStringExtras();

if you want to save data until user change it next time you can user SharedPrefrance as you have small amount of data.

and if your data is sensitive (want to provide more security) you can put in database or encrypt and put in database

as you are handling username and password of user you should go with sqlite database. otherwise SharedPrefrance was best.

here's my pref class:

public class GreetingCardData {
    public static final String SHARED_PREF_FILE     =   "greetingCardData";
    public static final String KEY_DO_NOT_SHOW      =   "doNotShow";
    public static final String KEY_CATEGORIES_JSON  =   "categoriesJson";   
    private SharedPreferences sharedPrefs;
    private Editor prefsEditor;

    public GreetingCardData(Context context) {
        this.sharedPrefs = context.getSharedPreferences(SHARED_PREF_FILE, 0);
        this.prefsEditor = sharedPrefs.edit();
    }   

    public void setDoNotShowFlag ( boolean flag ){
        prefsEditor.putBoolean( KEY_DO_NOT_SHOW, flag );
        prefsEditor.commit();
    }

    public boolean getDoNotShowFlag(){
        return sharedPrefs.getBoolean( KEY_DO_NOT_SHOW, false );
    }

    public void setGreetingcardJson( String jsonString ){
        prefsEditor.putString( KEY_CATEGORIES_JSON, jsonString );
        prefsEditor.commit();
    }

    public String getGreetingcardJsonString(){
        return sharedPrefs.getString(KEY_CATEGORIES_JSON, "");
    }    
}

call from Activity: to save data:

new  GreetingCardData(ActivityMain.this).setDoNotShowFlag(flag);

to get data:

boolean flag =  new  GreetingCardData(ActivityMain.this).getDoNotShowFlag();