Best Method to Save and Read Data in Android

前端 未结 1 505
迷失自我
迷失自我 2021-01-17 00:41

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

相关标签:
1条回答
  • 2021-01-17 01:27

    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();
    
    0 讨论(0)
提交回复
热议问题