Importing a static database in Android improves the application startup speed for the first time.

Source: Internet
Author: User

  The first impression of an android application is very important. In addition to a good creative and beautiful interface, performance is also a key part. This article discusses the speed of the first startup. The Startup Process of the android application does not allow users to wait too long. BestWithin 3 seconds . In general, content Initialization is one of the main factors that affect the first startup speed of Android applications, especially when creating databases and inserting a certain number of initial records, the best way is to import a static database during initial initialization.

Importing a static database in Android is very easy. First, put the prepared static database file under the raw subdirectory In the res directory of the android project. If this subdirectory is not available, manually create the directory, then, in the application initialization stageCodeCopy database filesUnder a specific directory, assume that the package name of the Android Application is com. Test. In most cases, the default database file of the application is located under the/data/COM. Test/databases directory.

 String dbdirpath = "/data/COM. Test/databases "; 
File dbdir =NewFile (dbdirpath );
If(! Dbdir. exists ())//If this directory does not exist, create
Dbdir. mkdir ();
//Open the input stream of the static database file
Inputstream is = context. getresources (). openrawresource (R. Raw. data );
//Open the output stream of the target database file
Fileoutputstream OS =NewFileoutputstream (dbdirpath + "/data. DB ");
Byte[] Buffer =New Byte[1024];
IntCount = 0;
//Copy static database files to the destination
While(COUNT = is. Read (buffer)> 0 ){
OS. Write (buffer, 0, count );
}
Is. Close ();
OS. Close ();

According to a recently completed application, after importing a static database, the first start time has changed from nearly 4 seconds to 1 second, and the effect is quite remarkable.

However, this method assumes that the application installation directories of all Android devices are the same, and the database file directories are/data/package name/databases, however, the android documentation does not explicitly stipulate that all devices have such a directory structure, so the static database file is copied toThere is still some danger in the method of setting a dead directory. A better solution is to use the API provided by the Android system to solve this problem. In short, we should avoid using a fixed directory. Below is a better copy process:

 //Open the input stream of the static database file
Inputstream is = context. getresources (). openrawresource (R. Raw. data );
//Use the context class to open the output stream of the target database file, which can avoid writing the path to death.
Fileoutputstream OS = context. openfileinput ("data. DB ");
Byte[] Buffer =New Byte[1024];
IntCount = 0;
//Copy static database files to the destination
While(COUNT = is. Read (buffer)> 0 ){
OS. Write (buffer, 0, count );
}
Is. Close ();
OS. Close ();

The final database file will be located at/data/COM. in the data/Files directory, note that when you use the openorcreatedatabase method of the context class or the sqliteopenhelper tool class, you cannot pass the database name as a parameter, instead, you need to pass the full path of the database file to it..

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.