The way Android stores data without using a database _android

Source: Internet
Author: User

This example describes how Android stores data without using a database. Share to everyone for your reference. The specific analysis is as follows:

In some cases we do not need to build a database, but we want to save some data, wait until the next time the program is run, then how do we do?

1. Referencing namespaces

Import android.content.SharedPreferences;

2. Define a new class Pictureglobaldef to store the data, defined in the class:

Public final static String appsetting = "Settingfile";
Public final static String default_switch_mode_key= "Default_switch_mode";
public static Boolean switch_open = false;

3. Where you want to refer to the data switch_open:

Sharedpreferences Settingviewmode = getsharedpreferences (
 picturenoteglobaldef.appsetting, 0);
Boolean bswitch = Settingviewmode.getboolean (
 picturenoteglobaldef.default_switch_mode_key,
 Picturenoteglobaldef.switch_open);

4. Where you want to save the data switch_open:

Picturenoteglobaldef.switch_open = Bswitch;
Sharedpreferences Settingviewmode = getsharedpreferences (appsetting,0);
Sharedpreferences.editor Editor = Settingviewmode.edit ();
Editor.putboolean (Picturenoteglobaldef.default_switch_mode_key,
 picturenoteglobaldef.switch_open); 
Editor.commit ();

5. Read and write the sharedpreferences of other applications

Sometimes we need to read and write the sharedpreferences of other applications, what should we do then?

The key to reading sharedpreferences other applications is to get the context of other applications:

Context tempcontext = null;
Tempcontext = Createpackagecontext ("a.b", context.context_ignore_security);
Here A.B represents the package name for the application

This gets the context of the other application.

After getting to the context, you can use the Getsharedpreferences method of the context to get the Shaerdpreferences object, which reads and writes data according to the 1-4 method.

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.