android-mobile phone attribution and landline land search

Source: Internet
Author: User

When developing an Android project, there was a need to query the phone and landline attribution, but Android itself did not find a database to provide a place to query, so we needed to find a way to meet the needs of the project.

My initial idea is to put these attribution data into an ordinary file, the content of the file can be in the format of JSON format, and then each time the app launches the JSON content of the file into Java object, Google provides a tool, We can directly convert the JSON string into our defined class class, which is much better than the Java-provided jsonobject, but I consider the size of the database and the efficiency of the query, or decide to use a database query.

Goole's tool for converting JSON is Gson, download link: http://download.csdn.net/detail/qxs965266509/8367275
How to use: http://blog.csdn.net/qxs965266509/article/details/42774691

First, you need to download the database of the attribution: http://download.csdn.net/detail/qxs965266509/8367219

Store database DB files in assets or raw files

After startup, the DB file needs to be copied to the SD card in its own definition of the directory, of course, you may wonder why the other people are copied to the data file in the project file in the databases file, and I did not do this? The reason is: To view the directory requires root permissions, Of course this is not the main reason, but in my test when I copied the entire DB file into the project file under the data file, and did not copy the entire DB file in the past, but part, I think it should be too large for the reason, so consider copying to the SD card.

Copy the code as follows:

        public static final String db_name = "Callhomedb.db";p rivate int buffer_size = 1024;private string Getdirpath () {string PA th = FileHandler.sdcardFolder.getAbsolutePath (); return path;} private void Copycontactlocation () {try {String path = Getdirpath () + "/" + db_name; File File = new file (path), if (!file.exists ()) {InputStream is = MainActivity.instance.getResources (). Getassets (). Open ( DB_NAME); FileOutputStream fos = new FileOutputStream (path); byte[] buffer = new Byte[buffer_size];int count = 0;while ((count = IS.R EAD (buffer)) > 0) {fos.write (buffer, 0, count);} Fos.close (); Is.close ();}} catch (FileNotFoundException e) {log.e ("File not Found"); E.printstacktrace ();} catch (IOException e) {log.e ("IO Exception "); E.printstacktrace ();}}
Here's the query:

There is a way to convert a db file directly into a database object

    Sqlitedatabase db = sqlitedatabase.openorcreatedatabase (path, NULL);

However, you can directly manipulate the Sqlitedatabase instance to query the database, if you have a very small number of queries, you can use this, do not see the query efficiency, query the number of large words is not recommended in this way, query efficiency is too low, seriously affect the implementation of the project framework of the efficiency, A lot of things can become less sensitive.


In order to improve query efficiency, I use Android to provide query database Sqliteopenhelper class, query efficiency greatly improved, believe that Android is doing a lot of optimization.

First you need to rewrite the context of the database, as follows:

Package Com.open.lxxms.handler;import Java.io.file;import Com.open.lib.mylog;import android.content.context;import Android.content.contextwrapper;import Android.database.databaseerrorhandler;import Android.database.sqlite.sqlitedatabase;import android.database.sqlite.sqlitedatabase.cursorfactory;/** * Customdatabasecontext 2015-1-10 PM 7:51:05 * * @author Choshaosong [email protected] */public class Customdatabasecontext ext Ends Contextwrapper {public String tag = "Customdatabasecontext";p ublic MyLog log = new MyLog (tag, true);p rivate String MD Irpath;public Customdatabasecontext (Context base, String Dirpath) {super (base); This.mdirpath = DIRPATH;LOG.E (" Custompathdatabasecontext ");} @Overridepublic file Getdatabasepath (String name) {file result = new File (Mdirpath + file.separator + name); if (!result.ge Tparentfile (). exists ()) {Result.getparentfile (). Mkdirs (); return result;} @Overridepublic sqlitedatabase openorcreatedatabase (String name, int mode,cursorfactory factory) {LOG.E (" OpenorcreatedaTabase1 "); return Sqlitedatabase.openorcreatedatabase (Getdatabasepath (name), factory);} @Overridepublic sqlitedatabase openorcreatedatabase (String name, int mode,cursorfactory factory, Databaseerrorhandler ErrorHandler) {log.e ("OpenOrCreateDatabase2"); return Sqlitedatabase.openorcreatedatabase (Getdatabasepath (name). GetAbsolutePath (), factory, ErrorHandler);}}

Here is the tool class for my encapsulated DB:

Package Com.open.lxxms.handler;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Android.content.Context; Import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.sqliteopenhelper;import Android.os.environment;import com.open.lib.mylog;import com.open.lxxms.mainactivity;/** * DBOptions 2015-1-10 PM 8 : 57:47 * * @author Choshaosong [email protected] */public class Dbhandler extends Sqliteopenhelper {public String tag = "DBO Ptions ";p ublic MyLog log = new MyLog (tag, true);p ublic static final String db_name =" callhomedb.db ";p rivate int Buffer_si ZE = 1024;public static final String package_name = "Com.open.lxxms";p ublic static final String db_path = "/data" + Environ Ment.getdatadirectory (). GetAbsolutePath () + "/" + package_name;public static Dbhandler getinstance (context context) { DbhandleR dboptions = new Dbhandler (context, NULL, NULL, 1); return dboptions;} Public Dbhandler (Context context, String name, cursorfactory factory,int version) {//Super (context, name, factory, Versio n); Super (new Customdatabasecontext (Context, Getdirpath ()), db_name,factory, version); try {String path = Getdirpath () + " /"+ db_name; File File = new file (path), if (!file.exists ()) {InputStream is = MainActivity.instance.getResources (). Getassets (). Open ( "Callhomedb.db"); FileOutputStream fos = new FileOutputStream (path); byte[] buffer = new Byte[buffer_size];int count = 0;while ((count = IS.R EAD (buffer)) > 0) {fos.write (buffer, 0, count);} Fos.close (); Is.close ();}} catch (FileNotFoundException e) {log.e ("File not Found"); E.printstacktrace ();} catch (IOException e) {log.e ("IO Exception "); E.printstacktrace ();} LOG.E ("Dboptions");} public static Filehandler Filehandler = Filehandler.getinstance ();p rivate static String Getdirpath () {String path = Fileha Ndler.sdcardFolder.getAbsolutePath (); return path;} @Overridepublic void OnCreate (Sqlitedatabase db) {//TODO auto-generated method STUBLOG.E ("OnCreate");} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//TODO auto-generated method stublog.e ("Onupgrade");} Public String GetResult (sqlitedatabase database, string number) {//LOG.E (number); string result = "Unknown number"; # = Number.replaceall ("", "");//try {if (Number.length () > 7) {String Firstnum = Number.s Ubstring (0, 1), if (Number.length () >=) {if ("0". Equals (Firstnum)) {String S1 = number.substring (1); String s2 = S1; String second = s1.substring (0, 1), if (Second.equals ("1") | | second.equals ("2")) {s2 = s1.substring (0, 2);} else {s2 = s1. SUBSTRING (0, 3);} String sql = "Select location from tel_location where _id =?"; string[] param = new string[] {s2};if (database! = null && database.isopen ()) {cursor cursor = Database.rawquery (SQL, param), if (Cursor.movetonext ()) {result = cursor.getstring (0);} Cursor.close ();}} else {if (Number.indexof ("+") = = 0) {Number = number.substring (3);} if (Number.indexof ("") = = 0) {Number = number.substring (2);} String S1 = number.substring (0, 7); String sql = "Select location from mob_location where _id =?"; string[] param = new string[] {s1};if (database! = null && database.isopen ()) {cursor cursor = Database.rawquery (SQL, param), if (Cursor.movetonext ()) {result = cursor.getstring (0);} Cursor.close ();}}} else {result = "local number";}} else {if (Number.length () < 4) {result = "unknown number";} else {result = "local number";}} } catch (Exception e) {//E.printstacktrace ();//}return result;}}

How to use:

Dbhandler dboptions = dbhandler.getinstance (context); Sqlitedatabase database = Dboptions.getreadabledatabase ();
String result = Dboptions.getresult (database, number);


Database of the place of attribution: http://download.csdn.net/detail/qxs965266509/8367219


If reproduced, please be famous from http://blog.csdn.net/qxs965266509


android-mobile phone attribution and landline land search

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.