The first impression of an Android app is very important to the user, in addition to having a good creative and beautiful interface, performance is also a key part of this article is to discuss the first startup speed problem. Android app start-up process does not allow users to wait too long, personally feel best control within 3 seconds. In general, the initialization of content is one of the main factors that affect the first boot 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 the static database at the first initialization.
Importing a static database in Android is simple, first put the prepared static database files in the Android project's res directory under the raw subdirectory, if there is no such subdirectory, create the directory manually, Then in the initialization phase of the app, copy the database files to a specific directory with code similar to the following, assuming that the Android app's package name is Com.test, then in most cases the default database file for the app is located in/data/data/com.test/ Under the Databases directory.
String Dbdirpath = "/data/data/com.test/databases";
File Dbdir = new file (Dbdirpath);
if (!dbdir.exists ())//If the directory does not exist then create
Dbdir.mkdir ();
Open the input stream of a static database file
InputStream is = Context.getresources (). Openrawresource (R.raw.data);
Open the output stream for the target database file
FileOutputStream OS = new FileOutputStream (dbdirpath+ "/data.db");
byte[] buffer = new byte[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 ();
In a recently completed application, with the introduction of a static database, the first boot time from nearly 4 seconds into 1 seconds, the effect is still very obvious.
However, this approach assumes that all Android devices have the same application installation directory, and that the directory for the database files is/data/data/package name/databases, but the Android document does not explicitly stipulate that all devices have this directory structure, So copying a static database file to a pre-determined directory is still a risky practice. A better approach is to use the API provided by the Android system to solve this problem, in short, we have to avoid the use of fixed directory, the following is a better copy process:
Open the input stream of 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, which avoids writing the path to death.
FileOutputStream OS = context.openfileinput ("data.db");
byte[] buffer = new byte[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 is important to note that when using the Openorcreatedatabase method of the context class or the Sqliteopenhelper tool class, Instead of passing the names of the databases as parameters, you pass the full path of the database files to them.
Increase your application's first startup speed by importing a static database in Android