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

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

问题描述

我有,使用代理服务器来浏览URL连接的应用程序。但是,代理主机,端口,用户名和密码,我使用的是使用硬code。我已经实现了。但现在我想以取代硬code,只需引用该数据的对象。因此,用户可以输入他们想要与可以使用他们的用户名和密码的代理。这里是我的流程是什么样子。

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).

谢谢

推荐答案

如果你只是想将数据发送到下一个actvity您可以使用 intent.putExtra(); 和在接下来的活动呼叫 getIntent()getStringExtras();

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

如果你想保存数据,直到用户改变它下一次你可以用户共享prefrance 你有少量的数据。

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

因为你正在处理的用户名和密码,你应该去与SQLite数据库。否则共享prefrance 是最好的。

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

这是我的preF级:

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);

要获取数据:

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

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

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