After the CPE is ported to the framework, the original database storage problem is resolved.

Source: Internet
Author: User

Recently, the company's business needs, the original apk form of CPE, into the framework layer, that is, stripping out the CPE, made a long time, and finally have time to write a blog, make a note to meet the problem.

The first problem is that some of the original apk, after porting to the framework layer, the environment is different, you must make some changes.

For example

1.ContentProvider, because not provided to other program data sharing, or removed, all changed to SQLite direct call. Either peel this contentprovider out, form an apk, fix it to the framework, and provide contentprovider for the CPE.

2.SharedPreferences, after entering the framework, Sharedpreferences has no place to store the XML file generated by it, so it should be removed, and then how to save it to save the data, such as terminal startup information and initialization information. Finally decided to use a file to save, depending on the properties of the tool class read and write data, properties initially encountered the problem of writing data, and later resolved. Mainly Mproperties.setproperty (name,value); Just write the data to the memory, and not write to the file, tangled half a day, modified a bit of code, OK.


try {
Os=new FileOutputStream (Configpath);

Mproperties.setproperty (Name,value);
Mproperties.store (OS, NULL);
} catch (Exception e) {
E.printstacktrace ();
} finally{
try {
Os.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

3.CPE The original configuration file, after the framework layer, the directory structure is different, you have to find a place to store these configuration files. Modified the directory where these profiles were referenced, and finally migrated to the/system/etc/cpe/directory.

4.CPE Dependent third-party jar package, the CPE enters the framework, it is itself a jar form exists, but by the systemservice into the context of other it. The third-party jar that the CPE relies on also has to go into the framework, when it is all imported into the framework at the beginning, after compiling and running, it is found that another apk on the board is referencing the CPE jar I imported, which is a conflict. To avoid this problem, decide to modify the CPE package name.

5. As the db file generated by sharedpreferences,apk, by default in the/data/data/package name/database/directory, and after entering the framework, the DB file to find a place to store, in order to solve this problem, find a lot of information, Tested for half a day, finally realized, and the db file is stored in the/data/cpe/directory. Because it is the APK in the test, did not break into the framework, the directory on the Board is not authorized to write data, can only be tested with a U disk, written in the/sdcard/directory test. Said for a long, that is, the specified directory to hold the db file.

Define helper classes to inherit Sqliteopenhelper:

public class Databasehelper extends Sqliteopenhelper

Construction Method:

private static final String PATH = "/SDCARD/CPE";

Public Databasehelper (Context context, String name) {
Super (new Custompathdatabasecontext (context, PATH), name, NULL, VERSION);
}

Note the Custompathdatabasecontext.java in the construction method:


public class Custompathdatabasecontext extends contextwrapper{

private static final String TAG = CustomPathDatabaseContext.class.getSimpleName ();

Private String Mdirpath;
Public Custompathdatabasecontext (Context base,string name) {
Super (base);
Mdirpath = name;
}

@Override
Public File Getdatabasepath (String name) {
File result = new file (Mdirpath + file.separator + name);
if (!result.getparentfile (). exists ())
{
Result.getparentfile (). Mkdirs ();
}
return result;
}

@Override
Public sqlitedatabase openorcreatedatabase (String name, int mode, Cursorfactory Factory)
{
LOG.D (TAG, "openorcreatedatabase");
Return Sqlitedatabase.openorcreatedatabase (Getdatabasepath (name), factory);
}
@Override
Public sqlitedatabase openorcreatedatabase (String name, int mode, Cursorfactory factory, Databaseerrorhandler ErrorHandler) {
LOG.D (TAG, "openorcreatedatabase ...");
Return Sqlitedatabase.openorcreatedatabase (Getdatabasepath (name). GetAbsolutePath (), factory, ErrorHandler);
}
}

Why do you define this, look at the source code, Contextwrapper is the implementation class of the context, after getreadabledatabase (), or getwriteabledatabase () call, Executes Openorcreatedatabase (), after version 4.0 executes the second openorcreatedatabase (), which specifies the directory for DB.


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.