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 SharedPreferences in Android

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

Share

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

This article introduces the knowledge of "how to use SharedPreferences in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Android SharedPreferences detailed explanation

There are two ways to obtain SharedPreferences:

1 call the getSharedPreferences () method of the Context object

2 call the getPreferences () method of the Activity object

The difference between the two ways:

The SharedPreferences object obtained by calling the getSharedPreferences () method of the Context object can be shared by other components under the same application.

The SharedPreferences object obtained by calling the getPreferences () method of the Activity object can only be used in that Activity.

Four modes of operation of SharedPreferences:

Context.MODE_PRIVATEContext.MODE_APPENDContext.MODE_WORLD_READABLEContext.MODE_WORLD_WRITEABLE

Context.MODE_PRIVATE: the default operation mode, which means that the file is private data and can only be accessed by the application itself. In this mode, the content written will overwrite the contents of the original file.

Context.MODE_APPEND: the mode checks whether the file exists and appends the content to the file if it exists, otherwise a new file is created.

Context.MODE_WORLD_READABLE and Context.MODE_WORLD_WRITEABLE are used to control whether other applications have permission to read and write the file.

MODE_WORLD_READABLE: indicates that the current file can be read by other applications.

MODE_WORLD_WRITEABLE: indicates that the current file can be written by other applications.

Save the data to SharedPreferences:

SharedPreferences preferences=getSharedPreferences ("user", Context.MODE_PRIVATE); Editor editor=preferences.edit (); String name= "xixi"; String age= "22"; editor.putString ("name", name); editor.putString ("age", age); editor.commit ()

Read data from SharedPreferences:

SharedPreferences preferences=getSharedPreferences ("user", Context.MODE_PRIVATE); String name=preferences.getString ("name", "defaultname"); String age=preferences.getString ("age", "0"); what is Android

Android is a free and open source operating system based on the Linux kernel, mainly used in mobile devices, such as smartphones and tablets, led and developed by Google and the Open Mobile Alliance.

This is the end of the content of "how to use SharedPreferences in Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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