在Android中保存和读取数据的最佳方法 [英] Best Method to Save and Read Data in Android

查看:220
本文介绍了在Android中保存和读取数据的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程式使用Proxy来浏览网址连结。但代理主机,端口,用户名和密码,我使用的是使用硬编码。我已经实现了。但现在我想替换硬编码只是一个对象引用的数据。因此用户可以输入他们想要的代理,并可以使用他们的用户名和密码。





因此,第一个用户可以输入他们想要的连接,主机,端口等的名称。然后,保存它。然后在下一个活动中,它将读取该连接的数据,如果存在,用户只需点击并浏览它。如果没有,他们可以创建连接。



感谢,

解决方案

如果你只是想发送数据到下一个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();

这篇关于在Android中保存和读取数据的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆