[Development Diary of Android in a row from scratch] (8) -- Android data storage (II) and android Data Storage

Source: Internet
Author: User

[Development Diary of Android in a row from scratch] (8) -- Android data storage (II) and android Data Storage
Let's talk about database operations. Come On!When talking about data storage, databases have to be mentioned. Databases are the best tool for storing relational data. Android provides powerful database support for developers to easily construct database-based applications. Android database applications rely on SQLite, the most popular open source embedded database. In Android, the database file of an application is private to the application and stored in the databases subdirectory under the application data directory. From the code structure, Android database implementation can be divided into two layers. At the underlying layer, C ++ calls the SQLite interface to execute SQL statements, and exposes Java accessible interfaces through JNI.I. Android database usage

Android uses android. database. sqlite. SQLiteDatabase to represent a database object. It provides two modes to help developers perform basic database operations, such as addition, deletion, modification, and query.

Ii. cloud services for Android data

In essence, cloud storage stores data on mobile devices to remote servers over the network. In Android, some auxiliary functions are added to simplify the implementation of the entire process. The first is to identify a user using a Google account. In android, Google accounts are supported by default as the user identity, and all applications in the system can obtain user logon information through the account system. Second, with a Google account, developers do not need to build their own backend service systems.

Android's cloud data access is centrally managed by the System Service BackupManagerService. When an application submits a backup data request, BackupManagerService puts the request in the backup queue, which is submitted to the cloud according to certain control logic. When a new application is installed on the system, a data recovery event is triggered. BackupManagerService extracts the backup data from the cloud based on the application package name and user account and tries to recover the data.

In practice, Android constructs a subclass android. app. backup. BackupAgentHelper derived from the BackupAgent class to build cloud storage components more conveniently.

import java.io.File;
import java.io.IOException;

import android.app.backup.BackupAgentHelper;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.app.backup.FileBackupHelper;
import android.os.ParcelFileDescriptor;



public class MyBackupAgent extends BackupAgentHelper {

    private static final String KEY = "my_backup";
    
    @Override
    public void onCreate () {
        // Construct file read and write objects and declare files to be backed up
        FileBackupHelper helper = new FileBackupHelper (this, "backup_file");
        addHelper (KEY, helper);
        super.onCreate ();
    }
    
    @Override
    public void onBackup (ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor newState) throws IOException {
        // Call the parent method and submit the entire file to the cloud
        super.onBackup (oldState, data, newState);
    }

    @Override
    public void onRestore (BackupDataInput data, int appVersionCode,
            ParcelFileDescriptor newState) throws IOException {
        // Call the parent method to overwrite the local file from the cloud
        super.onRestore (data, appVersionCode, newState);
    }

    @Override
    public void onRestoreFile (ParcelFileDescriptor data, long size,
            File destination, int type, long mode, long mtime)
            throws IOException {
        // TODO Auto-generated method stub
        super.onRestoreFile (data, size, destination, type, mode, mtime);
    } 

Android does not submit data to the cloud by itself. developers need to explicitly call the dataChanged function of android. app. backup. BackupManager to trigger the data.

Like all components, cloud storage components are hosted by the system. In this case, you need to put the component information into the configuration file.

<application android:backupAgent = "MyBackupAgent" ...>

 

Here, the data section is almost explained, and the rest will return to our data source component ContentProvider.Data is critical to developers. How to use it and how to make it the most efficient is also the focus of our technicians. Android Backup service management mechanism and Architecture Analysis http://blog.csdn.net/goohong/article/details/8026045
Resource download (database + file) demo ==========================================================Author: cpacm
(Http://www.cnblogs.com/cpacm/p/3922984.html)  
Data storage problems in Android Development

First of all, let me talk about it more. Let's take two steps first.
1. Network Access
2. Data Storage
I. Android Network Access is generally an imitation of the Http protocol. Simply put, it is the application of the HttpGet class and the HttpPost class. Then you give the address to it, and the http protocol will return a response, this response includes the return code and content, and the content you want is included in the returned content. So here is your first question: file or data? What is actually returned depends on what the server interface returns to you. Generally, an enterprise returns a string data in xml format or a string data in Json format, the content of your questions will be included in this article. Another possibility is that you initially accessed not a server interface, but a network file address. If so, if you want to obtain it, you need to use the JAVA input/output stream technology to read and write files on the network. Then, it is a file.
II. Data Storage
There are a total of five types of data storage for android. You can check them for details, but here I will mention that if you want to close the data, you should create a database, then insert the data into the database. If it is a file, you need to convert the binary data and store it again, so that it becomes confidential. If you want to put the data in your mobile phone card, so we need to have the first permission here, because you are a process from scratch, you have to create it without this file, and then write the file data to the file using the JAVA input/output stream technology, after the storage is complete or after writing, the entire operation is complete.
Please cherish it and contact me again if you do not understand it.

Help write a program (android development), user interaction with the interface, online data, so that online data is stored in json format on the mobile phone

You can encapsulate the obtained data into a java bean in toJson.

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.