General settings in Android?

I am a novice Android developer who is currently trying to create a login screen.

I need to find the easiest way to save the username and password in 1 class and get it from another class. See that Google provided several ways: http://developer.android.com/guide/topics/data/data-storage.html

which one is the most efficient and easy to code?

thanks!

+4
source share
5 answers

For login tasks, such as saving a username and password, you can use the general settings. Here I created my own methods for using general settings. Call the savePreferences() method and place your key and value (since savePreferences() is XML based), call Load with your key in the same way. And finally, don't forget to call deletePreferences() in LOGOUT.

 /** * Method used to get Shared Preferences */ public SharedPreferences getPreferences() { return getSharedPreferences(<PREFRENCE_FILE_NAME>, MODE_PRIVATE); } /** * Method used to save Preferences */ public void savePreferences(String key, String value) { SharedPreferences sharedPreferences = getPreferences(); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(key, value); editor.commit(); } /** * Method used to load Preferences */ public String loadPreferences(String key) { try { SharedPreferences sharedPreferences = getPreferences(); String strSavedMemo = sharedPreferences.getString(key, ""); return strSavedMemo; } catch (NullPointerException nullPointerException) { Log.e("Error caused at TelaSketchUtin loadPreferences method", ">======>" + nullPointerException); return null; } } /** * Method used to delete Preferences */ public boolean deletePreferences(String key) { SharedPreferences.Editor editor=getPreferences().edit(); editor.remove(key).commit(); return false; } 

Hope this helps you. Do not forget to do +1.

+4
source

Define some statistics to store the preference file name and keys that you intend to use:

 public static final String PREFS_NAME = "MyPrefsFile"; private static final String PREF_USERNAME = "username"; private static final String PREF_PASSWORD = "password"; 

Then you saved the username and password as follows:

 getSharedPreferences(PREFS_NAME,MODE_PRIVATE) .edit() .putString(PREF_USERNAME, username) .putString(PREF_PASSWORD, password) .commit(); 

So, you would get them like this:

 SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE); String username = pref.getString(PREF_USERNAME, null); String password = pref.getString(PREF_PASSWORD, null); if (username == null || password == null) { //Prompt for username and password } 

Alternatively, if you do not want to specify a settings file, you can simply use the default value:

 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); 
+4
source

SQLite Database and SharedPreferences will work for your purpose. But I would suggest SharedPreferences, as they are very easy to use. Some ppl like to create a class to store such variables, but the advantage of SQLite and the SharedPreferences file is that the username and password of the user will be with you, even if the application is in the background and will be destroyed. Therefore, when a user returns to your application, you can sign them again without asking for a password. If the user explicitly decides to log out, you can simply delete the login information from the general settings file

+3
source

The easiest way to access information from other activities is "SharedPreferences". I highly recommend this method for storing and searching for username and password. Since you can access this information from anywhere in the application without any complications. Registration information may need to be reused in the application.

+1
source

Many applications can provide a way to capture user settings in the settings of a specific application or activity. To support this, Android offers a simple set of APIs.

Preferences are usually pairs of name values. They can be stored as “General Preferences” in various actions in the application (note that at present they cannot be shared between processes). Or it may be something that needs to be preserved for a specific activity.

General preferences: General settings can be used by all components (actions, services, etc.) for applications.

Processed actions: these settings can only be used in action and cannot be used by other application components.

General preferences:

General settings are controlled using the getSharedPreferences method of the Context class. Settings are saved in the default file (1) or you can specify the file name (2), which will be used to refer to the settings.

(1) This is how you get an instance when specifying a file name

  public static final String PREF_FILE_NAME = "PrefFile"; SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE); 

MODE_PRIVATE is the operating mode for the settings. This is the default mode and means that only the calling application will access the created file. Other two modes supported are: MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE, another application can read the created file, but cannot modify it. In the case of MODE_WORLD_WRITEABLE, other applications also have write permissions for the created file.

(2) The recommended method is to use the default mode without specifying a file name:

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 

Finally, once you have an instance of preferences, here is how you can get the saved values ​​from the settings:

  int storedPreference = preferences.getInt("storedInt", 0); 

To store values ​​in a preference file, you must use the SharedPreference.Editor object. An editor is a nested interface of the SharedPreference class.

  SharedPreferences.Editor editor = preferences.edit(); editor.putInt("storedInt", storedPreference); // value to store editor.commit(); 

The editor also supports methods such as remove () and clear () to remove the preference value from the file.

Activity Settings:

General settings can be used by other components of the application. But if you do not need to share preferences with other components and you want to have personal preferences. You can do this using the getPreferences () method of activity. The getPreference method uses the getSharedPreferences () method with the name of the activity class for the preference file name.

Below is the code to get the settings:

  SharedPreferences preferences = getPreferences(MODE_PRIVATE); int storedPreference = preferences.getInt("storedInt", 0); 

The code for storing values ​​is also the same as in the case of general settings.

  SharedPreferences preferences = getPreference(MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putInt("storedInt", storedPreference); // value to store editor.commit(); 

You can also use other methods, such as storing an activity state in a database. Note. Android also contains a package called android.preference. The package defines classes for implementing the application user interface.

To see a few more examples, check the Android Data Storage post on the developers website.

For more information, check out this link:

Permanent data storage in android

0
source

Source: https://habr.com/ru/post/1436801/


All Articles