Android-mobile phone home location and landline home location query

Source: Internet
Author: User

Android-mobile phone home location and landline home location query

During the development of the Android project, there was a need to query the mobile phone and landline locations. However, Android itself did not find a database that provides the query locations, therefore, we need to find a solution to meet the needs of the project.

My original idea was to store the stored data in a common file. The file content can be in JSON format, then, every time the App is started, the JSON content of the file is converted into a Java object. Google provides a tool that can directly convert the JSON string into a Class we have defined, it is much better than the JsonObject provided by Java. However, considering the size and query efficiency of the database, I decided to use the database query.

Goole's JSON Conversion Tool is Gson. Download link: www.bkjia.com
Usage: http://blog.csdn.net/qxs965266509/article/details/42774691

 

First, download the local database www.bkjia.com.

Store database DB files in assets or raw files

 

After the startup, You need to copy the DB file to the directory defined in the SD card. Of course, you may have questions, why did others copy data to the DataBases file in the project file under the data file, but I did not do this? The reason is: the root permission is required to view the directory. Of course, this is not the main reason, but when I copy the entire DB file to the project file under the data file during my test, I didn't copy the entire DB file, but part of it. I think it should be because the file is too large, so I want to copy it to the SD card.

The copy code is as follows:

        public static final String DB_NAME = callHomeDB.db;private int BUFFER_SIZE = 1024;private String getDirPath() {String path = 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.read(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();}}
The following describes the query:

 

There is a way to directly convert the DB file into a Database object.

 

    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(path, null);

However, you can directly operate the SQLiteDatabase instance to query the database. If you have a very small number of queries, you can use this method, but you cannot see the query efficiency, we recommend that you do not use this method if the number of queries is large. The query efficiency is too low, which seriously affects the execution efficiency of the project framework. Many things will become insensitive.

 

 


To improve the query efficiency, the SQLiteOpenHelper class of the query database provided by android has greatly improved the query efficiency. I believe android has made many optimizations.

First, rewrite the context of the DataBase. The Code is 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;/*** mmdatabasecontext 7:51:05 *** @ author Joe Xiaosong coolspan@sina.cn */public class CustomDa TabaseContext extends ContextWrapper {public String tag = CustomDatabaseContext; public MyLog log = new MyLog (tag, true); private String mDirPath; 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. getParentFile (). 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 );}}

 

The following is the tool class of the 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 Oid. OS. environment; import com. open. lib. myLog; import com. open. lxxms. mainActivity;/*** DBOptions 8:57:47 *** @ author Joe Xiaosong coolspan@sina.cn */public class DBHandler extends SQLiteOpenHelper {public String tag = DBOptions; public MyLog log = new MyLog (tag, true); public static final String DB_NAME = callHomeDB. db; private int BUFFER_SIZE = 1024; public static final String PACKAGE_NAME = com. op En. lxxms; public static final String DB_PATH =/data + Environment. 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, version); super (new CustomDatabas EContext (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. read (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 (); private static String getDirPath () {String path = fileHandler. sdcardFolder. getAbsolutePath (); return path;} @ Overridepublic void onCreate (SQLiteDatabase db) {// TODO Auto-generated method stublog. e (onCreate) ;}@ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int n EwVersion) {// TODO Auto-generated method stublog. e (onUpgrade);} public String getResult (SQLiteDatabase database, String number) {// log. e (number); String result = unknown number; number = number. replaceAll (,); // try {if (number. length ()> 7) {String firstNum = number. substring (0, 1); if (number. length ()> = 10) {if (0. equals (firstNum) {String s1 = number. substring (1); String s2 = s1; String second = s1.substri Ng (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 (+ 86) = 0) {number = number. substring (3);} if (number. indexOf (86) = 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 ;}}

 

Usage:

 

DBHandler dbOptions = DBHandler.getInstance(context);SQLiteDatabase database = dbOptions.getReadableDatabase();
String result = dbOptions.getResult(database, number);

 

 

Database of the home location: http://download.csdn.net/detail/qxs965266509/8367219

 

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

 

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.