Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use SharedPreference to realize data Storage in Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to use SharedPreference to achieve data storage in Android", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use SharedPreference to achieve data storage in Android" article.

Unlike file storage, SharedPreferences uses key-value pairs to store data

That is to say, when saving a piece of data, you need to provide a corresponding key to the data, so that when reading the data, you can use this key to read the corresponding value.

And SharedPrefencese also supports a variety of different data type storage. If the stored data type is an integer, then the extracted data is an integer, and if it is String, then it is also String.

It is obvious that it is more convenient to use SharedPreferences.

The following steps are required to store data to ShraedPreferences

Get the SharedPreferences object

Get the Editor object of the SharedPreferences object

Call the method of the Editor object to add data to the file

Submit data to complete storage operation

There are three ways to get a SharedPereferences object

Call the getSharedPreferences () method of the Context class

SharedPreferences p=getSharedPreferences ("myPeferecences", MODE_PRIVATE)

The first parameter in getSharedPereferences is the file name, and the second parameter is the operation mode. The default is MODE_PRIVATE (private file).

Call getPrefecences in Activity

SharedPreferences p=getPreferences (MODE_PRIVATE)

GetPreferences defaults to the name of the currently active class as the name of the SharedPreferences file.

Call the getDefaultSharedPreferences of the PreferencesManager class

SharedPreferences p=getDefaultSharedPreferences (this)

Its parameters are in the context, and the currently applied package is taken as the name of the SharedPerefences file by default

Get the Editor object of SharedPreferences

SharedPreferences.Editor=p.edit ()

Call the method of the Editor object to add data to the file

Editor.putString ("username", "adming"); editor.putInt ("id", 123)

Submit data to complete storage operation

Editor.apply ()

At this point, the whole SharedPreferences stored procedure is completed, and when we want to use it, we can use the getXXX method to take it out.

A.setText (p.getString ("username", ")); the above is about" how to use SharedPreference to achieve data storage in Android ". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report