号码归属地的查询

实现Android号码归属地的查询:

号码归属地的数据库可以从淘宝或者其他地方购买。数据库需要需要和程序一起打包放进apk中,所以数据库数据不能太大。可以对数据库进行优化。

   

                      表tb_address

 

                       表numinfo

优化方法:1.提取冗余数据

              2.拆分冗余数据到一张新的表中

              3.利用外键引用引用数据的表

 

第一步:将数据库db文件放到资产目录assert下,db文件会一起打包进项目中,但是不会一起安装。

           获取资产资源的方法:context.getAsserts().open("文件名")

public boolean copyFile(String name, File file, ProgressDialog pd) {
        try {
            AssetManager am = context.getAssets();
            InputStream is = am.open(name);
            FileOutputStream fos = new FileOutputStream(file);
            int len = 0;
            byte[] buffer = new byte[2048];
            int progress = 0;
            int total = is.available();
            pd.setMax(total);
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
                progress += len;
                pd.setProgress(progress);
            }
            pd.dismiss();
            is.close();
            fos.flush();
            fos.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

 

   第二步:数据库中号码归属地查询方法:

           判断是否是手机号码(正则表达式):String pattern = "^1[3458]\\d{9}$";

           手机号码归属地的查询:select city from address_tb where _id=(select outkey from numinfo where mobileprefix=?

           其他号码(根据区号来判断):select city from address_tb where area=? limit 1

           号码的格式:

              4位   :5556 模拟器
              10位 :3位的区号 + 7位的号码

              11位 :3位的区号 + 8位电话号码
                      4位的区号 + 7位电话号码

              12位 :4位的区号 + 8位的电话号码

package cn.itcast.mobilesafe.db.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/***
 * 号码归属查询
 * 
 * @author Administrator
 * 
 */
public class NumberQueryDao {

    public static String getAddress(String number) {
        String address = number;
        String path = "/data/data/cn.itcast.mobilesafe/files/naddress.db";

        SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READONLY);

        String pattern = "^1[3458]\\d{9}$";// 手机号码的格式
        if (db.isOpen()) {
            if (number.matches(pattern)) {
                String sql = "select city from address_tb where _id=(select outkey from numinfo where mobileprefix=?)";
                Cursor cursor = db.rawQuery(sql,
                        new String[] { number.substring(0, 7) });

                if (cursor.moveToFirst()) {
                    address = cursor.getString(0);
                }
                cursor.close();// 一定不要忘記了
            } else {//其他号码(固定电话)根据区号来判断
                Cursor cursor;
                switch (number.length()) {
                case 4:
                    address = "模拟器";
                    break;

                case 7:
                    address = "本地号码";
                    break;
                case 8:
                    address = "本地号码";
                    break;
                case 10:
                    cursor = db.rawQuery("select city from address_tb where area=? limit 1",
                            new String[] { number.substring(0, 3) });
                    if (cursor.moveToFirst()) {
                        address = cursor.getString(0);
                    }
                    cursor.close();
                    break;
                case 12:
                    cursor = db.rawQuery("select city from address_tb where area= ? limit 1 ",
                            new String[] { number.substring(0, 4) });
                    if (cursor.moveToFirst()) {
                        address = cursor.getString(0);
                    }
                    cursor.close();
                    break;
                case 11:
                    cursor = db
                            .rawQuery(
                                    "select city from address_tb where area = ? limit 1",
                                    new String[] { number.substring(0, 3) });
                    if (cursor.moveToFirst()) {
                        address = cursor.getString(0);
                    }
                    cursor.close();
                    cursor = db
                            .rawQuery(
                                    "select city from address_tb where area = ? limit 1",
                                    new String[] { number.substring(0, 4) });
                    if (cursor.moveToFirst()) {
                        address = cursor.getString(0);
                    }
                    cursor.close();
                    break;
                }
            }
        }

        return address;
    }
}

 

 

 

             

posted @ 2013-06-14 17:53  tagie  阅读(1062)  评论(0编辑  收藏  举报