QueryAddressUtils

来源:互联网 发布:大学生网络防诈骗 ppt 编辑:程序博客网 时间:2024/06/05 16:33
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class QueryAddressUtils {



/**
* 根据号码,查询地址
* @param ctx
* @param number
* @return
*/
public static String queryAddress(Context ctx,String number){

String result = "未知号码";

String dbPath = ctx.getFilesDir().getAbsolutePath()+"/address.db";
// 打开已经存在的数据库
SQLiteDatabase db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY);

if(isMobileNumber(number)){
Cursor cursor = db.rawQuery("select * from data2 where id = (select outkey from data1 where id= ? );",
new String[]{number.substring(0, 7)}); // 截取手机号码的前7位
// 查询数据库获得的cursor 是指向,第一行的,上一行
if(cursor.moveToNext()){
result = cursor.getString(cursor.getColumnIndex("location")); // 获得位置信息
}
cursor.close();
}else{
// 不是手机号
switch (number.length()) {
case 3: // 110 120 119 
result = "公共报警电话";
break;
case 4: // 5556 
result = "模拟机";
break;
case 5: // 10086  95515 95535 
result = "公共服务电话";
break;
case 7: // 
case 8: // 
result = "本地电话";
break;
case 11: //   021 12345678 
case 12: //   0371 1234567 
// 长途电话

// 假设区号是前三位
String areaCode = number.substring(1, 3);

Cursor cursor = db.query("data2", null, " area = ?", new String[]{areaCode}, null, null, null);
if(cursor.moveToNext()){ // 移动成功,说明查到了
result = cursor.getString(cursor.getColumnIndex("location"));
}

//  02141234567  不存在  0214 这样的区号

// 假设区号是前四位
areaCode = number.substring(1, 4);

cursor = db.query("data2", null, " area = ?", new String[]{areaCode}, null, null, null);
if(cursor.moveToNext()){ // 移动成功,说明查到了
result = cursor.getString(cursor.getColumnIndex("location"));
}
cursor.close();

break;
}
}

db.close();
return result;
}


/**
* 判断是否是手机号
* @param number
* @return
*/
private static boolean isMobileNumber(String number) {
/*
* 1- 以1开头
* 2- 第2位:  345678
* 3- 长度 11 位 

* ^1[345678][0-9]{9}$

*/

boolean isNum = number.matches("^1[345678][0-9]{9}$");

return isNum;
}
}
0 0
原创粉丝点击