1、把address.db(手机归属地数据库)在assets目录中。
2、在SplashActivity.java(应用启动时显示的动画界面)中把address.db拷贝到应用的files目录下。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//拷贝数据库
copyDB("address.db");
}
/**
* 把assert目录下文件拷贝到本地(/data/data/包名/files)
* @param dbName
* assert目录下的文件名
* @throws IOException
*/
private void copyDB(final String dbName) {
new Thread(){
public void run() {
//判断文件是否存在,如果存在不需要拷贝
File file = new File("/data/data/"+getPackageName() + "/files/" + dbName);
if (file.exists()) {//文件存在
return;
}
// 文件的拷贝
try {
filecopy(dbName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
private void filecopy(String dbName) throws IOException,
FileNotFoundException {
//io流来拷贝
//输入流
AssetManager assets = getAssets();
//读取assert的文件,转换成InputStream
InputStream is = assets.open(dbName);
//输出流
FileOutputStream fos = openFileOutput(dbName, MODE_PRIVATE );
//流的拷贝
//定义缓冲区大小10k
byte[] buffer = new byte[10240];
//读取的长度
int len = is.read(buffer);
int counts = 1;
//循环读取,如果读到文件尾部,返回-1
while (len != -1) {
//把缓冲区的数据写到输出流
fos.write(buffer,0,len);
//每次100k的时候刷新缓冲区的数据到文件中
if (counts % 10 == 0) {
fos.flush();//刷新缓冲区
}
//继续读取
len = is.read(buffer);
counts++;
}
fos.flush();
fos.close();
is.close();
}
3、电话归属地业务的封装类的实现
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
* 电话归属地业务的封装类
*
* @author Administrator
*
*/
public class PhoneLocationEngine {
public static String locationQuery(String phoneNumber, Context context) {
String location = phoneNumber;
//
/**
* 判断phoneNumber 1, 手机号 2, 固定电话 4008517517 3, 服务号码 110 120 95559 010 -
* 99999999 0391-3456654 0755-8888888
*/
// 如果是手机号
// 正则表达式
Pattern p = Pattern.compile("1{1}[3857]{1}[0-9]{9}");
Matcher m = p.matcher(phoneNumber);
boolean b = m.matches();
if (b) {
// 是手机号
location = mobileQuery(phoneNumber, context);
} else if (phoneNumber.length() >= 11) {
// 固定号码
// 如果是固定号码
location = phoneQuery(phoneNumber, context);
} else {
// 如果是服务号码
location = serviceNumberQuery(phoneNumber);
}
return location;
}
/**
* 查询服务号码 如:110匪警
*
* @param phoneNumber
* @return
*/
public static String serviceNumberQuery(String phoneNumber) {
String res = "";
if (phoneNumber.equals("110")) {
res = "匪警";
} else if (phoneNumber.equals("10086")) {
res = "中国移动";
}
return res;
}
/**
* @param phoneNumber
* 电话号码全称
* @param context
* @return 固定电话号码归属地
*/
public static String phoneQuery(String phoneNumber, Context context) {
/*
* phoneNumber 三种类型: 1, 手机号 2, 固定电话 3, 服务号码 110 120 95559 95555
* 0755-88888888 010-888888
*/
String res = phoneNumber;
SQLiteDatabase database = SQLiteDatabase.openDatabase(
"/data/data/"+getPackageName()+ "/files/address.db", null,
SQLiteDatabase.OPEN_READONLY);
String quHao = "";
// 2位区号 3位区号
if (phoneNumber.charAt(1) == '1' || phoneNumber.charAt(1) == '2') {
// 2位区号
quHao = phoneNumber.substring(1, 3);
} else {
// 3位区号
quHao = phoneNumber.substring(1, 4);
}
Cursor cursor = database.rawQuery(
"select location from data2 where area=?",
new String[] { quHao });
if (cursor.moveToNext()) {
res = cursor.getString(0);
}
return res;
}
/**
* @param phoneNumber
* 电话号码全称
* @param context
* @return 手机号码归属地
*/
public static String mobileQuery(String phoneNumber, Context context) {
/*
* phoneNumber 三种类型: 1, 手机号 2, 固定电话 3, 服务号码 110 120 95559 95555
*/
String res = phoneNumber;
SQLiteDatabase database = SQLiteDatabase.openDatabase(
"/data/data/"+getPackageName()+ "/files/address.db", null,
SQLiteDatabase.OPEN_READONLY);
Cursor cursor = database
.rawQuery(
"select location from data2 where id = (select outKey from data1 where id=?)",
new String[] { phoneNumber.substring(0, 7) });
if (cursor.moveToNext()) {
res = cursor.getString(0);
}
return res;
}
}
4、使用电话归属地业务的封装类查询电话归属地
//查询
String location = PhoneLocationEngine.locationQuery("15815151515", getApplicationContext());
address.db的下载地址:http://download.csdn.net/detail/csdn_lqr/9492589