Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use SharedPreferences in Android

Shulou Source: shulou.com Published: 2022-05-31 23:51:25 09月19日 Update

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!

Tags: Files objects content applications methods patterns data mobile phones ways more knowledge open practical free learned and then operating system representative company kernel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia Microsoft vpn Shulou Tech Info