An android In addition to the good creative and beautiful interface, performance is also a key part of this article is the first time to start the speed issue. The Android app's startup process does not allow users to wait too long, and individuals feel better to control it within 3 seconds. In general, the initialization of content is one of the main factors that affect the first startup speed of Android applications, especially when creating a database and inserting a certain number of initial records, the best way to do this is to import a static database on the first initialization.
Importing a static database on Android is simple, first putting the prepared static database files in the raw subdirectory of the Android engineering res directory, and manually creating the directory if there is no subdirectory. Then, in the initialization phase of the application, copy the database file to a specific directory using code similar to the following, assuming that the Android application's package name is Com.test, and in most cases the default database file is located in/data/data/com.test/ Databases directory below.
String Dbdirpath = "/data/data/com.test/databases";
File Dbdir = new file (Dbdirpath); if (!dbdir.exists ())
If the directory does not exist, create the
Dbdir.mkdir ();
Open the input stream for a static database file
InputStream is = Context.getresources (). Openrawresource (R.raw.data);
Open the output stream of the target database file
FileOutputStream OS = new FileOutputStream (dbdirpath+ "/data.db");
byte[] buffer = newbyte[1024];
int count = 0;
Copy a static database file to a destination
while ((count = is.read (buffer)) > 0) {
Os.write (buffer, 0, count);
}
Is.close ();
Os.close ();
With a recently completed application, the first startup time has changed from nearly 4 seconds to 1 seconds after importing a static database, and the effect is obvious.
However, this approach assumes that all Android devices have the same application installation directory, and that the directory of the database files is/data/data/package name/databases, but the Android documentation does not explicitly specify that all devices have such a directory structure. So it is dangerous to copy the static database files to a predetermined directory. A better approach is to use the API provided by the Android system to solve this problem, and in short, we want to avoid using a fixed directory, which is a better copy process:
Copy Code
Open the input stream for a static database file
InputStream is = Context.getresources (). Openrawresource (R.raw.data);
The context class is used to open the output stream of the target database file to avoid writing the path to death.
FileOutputStream OS = context.openfileinput ("data.db");
byte[] buffer = newbyte[1024];
int count = 0;
Copy a static database file to a destination
while ((count = is.read (buffer)) > 0) {
Os.write (buffer, 0, count);
}
Is.close ();
Os.close ();
}
The final database file will be located in the/data/data/com.data/files directory, and it should be noted that when using the Openorcreatedatabase method of the context class or the Sqliteopenhelper tool class, You can no longer pass the name of the database as a parameter, but instead pass the entire path of the database file to them.