Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android-手機歸屬地和座機歸屬地查詢

Android-手機歸屬地和座機歸屬地查詢

編輯:關於Android編程

在開發Android項目 的時候,有個查詢手機和座機歸屬地的需求,然而Android本身並沒有發現提供查詢歸屬地的數據庫,所以就需要我們自己來想辦法查詢來滿足項目的需求。

我最初的想法,就是把這些歸屬地數據存到一個普通的文件,文件內容可以用JSON格式的數據格式,然後每次啟動App的時候把文件的JSON內容轉換成Java的對象,Google提供了一款工具,可以直接把JSON的字符串轉換成我們定義好的Class類,比Java提供的JsonObject好很多,然而我考慮到數據庫的大小和查詢效率,還是決定使用數據庫查詢。

Goole的轉換JSON的工具是Gson,下載鏈接:www.2cto.com
使用方法:http://blog.csdn.net/qxs965266509/article/details/42774691

 

首先,需要下載歸屬地的數據庫:www.2cto.com

把數據庫DB文件存放到assets或者raw文件中

 

在啟動啟動後,需要把DB文件拷貝到Sd卡的中自己定義的目錄中,當然你可能會疑問,為什麼別人的都是拷貝到data文件下的項目文件中的DataBases文件中,而我卻沒有這麼做?原因是:查看該目錄需要root權限,當然這不是主要原因,而是在我測試的時候我把整個DB文件復制到data文件下的項目文件中時,並沒有把整個DB文件復制過去,而是一部分,我覺得應該是文件太大的原因,所以考慮復制到SD卡中。

復制代碼如下:

        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();
		}

	}
下面說一下查詢:

 

有一種方法可以直接把DB文件轉換成一個Database數據庫對象

 

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

然而,可以直接操作SQLiteDatabase的實例來查詢數據庫,如果你的查詢次數非常少的情況,可以使用這種,看不出查詢效率,查詢次數大的話建議不要用這種方式,查詢效率太低,嚴重影響真個項目框架的執行效率,很多東西都會變得不靈敏。

 

 


為了提高查詢效率,我使用的android提供的查詢數據庫的SQLiteOpenHelper類,查詢效率大大提高,相信android是做了很多優化的。

首先需要重寫DataBase的上下文,代碼如下:

 

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 下午7:51:05
 * 
 * @author 喬曉松 [email protected]
 */
public class CustomDatabaseContext 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);
	}

	@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.e(openOrCreateDatabase1);
		return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name),
				factory);
	}

	@Override
	public SQLiteDatabase openOrCreateDatabase(String name, int mode,
			CursorFactory factory, DatabaseErrorHandler errorHandler) {
		log.e(openOrCreateDatabase2);
		return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name)
				.getAbsolutePath(), factory, errorHandler);
	}
}

 

下面是我封裝的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 下午8:57:47
 * 
 * @author 喬曉松 [email protected]
 */
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.open.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 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.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;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		log.e(onCreate);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		log.e(onUpgrade);
	}

	public String getResult(SQLiteDatabase database, String number) {
		// log.e(number);
		String result = 未知號碼;
		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.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(+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 = 本地號碼;
			}
		} else {
			if (number.length() < 4) {
				result = 未知號碼;
			} else {
				result = 本地號碼;
			}
		}
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

		return result;
	}
}

 

使用方法:

 

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

 

 

歸屬地的數據庫:http://download.csdn.net/detail/qxs965266509/8367219

 

如有轉載,請著名出自http://blog.csdn.net/qxs965266509

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved