Android中利用数据库查询电话归属地

本文介绍了如何在Android应用中使用数据库查询电话归属地。首先将归属地数据库放入assets目录,接着复制到data/data应用目录下,创建AddressDao类进行数据库操作。虽然对于3位和4位号码查询不完全,但提供了基本的查询流程。

1.首先将我们的归属地的数据库放入assets/address.db
2.然后需要将我们的数据库文件copy到
data/data/com.ittest.address/files/address.db

private void copyDB(String dbName) {//dbName文件名
        File destFile = new File(getFilesDir(),dbName);
        //System.out.println(getFilesDir());

        if(destFile.exists()){//如果存在就不需要再次copy了
            System.out.println("数据库已存在");
            return;
        }

        InputStream in = null;
        FileOutputStream out = null;
        try {
            in = getAssets().open(dbName);
            out = new FileOutputStream(destFile);
            int len = 0;
            byte[] buffer = new byte[1024];

            while((len = in.read(buffer))!= -1){
                out.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

3.创建一个AddressDao.java,进行数据库查询操作
手机匹配较为简略,但是大致能够覆盖,但是对于3位,4位号码由于缺少数据库的支持,所以只是写个大概,知道意思就行了。

public class AddressDao {

    private static final String PATH = "data/data/com.ittest.address/files/address.db";// 注意该路径必须是data/data否则数据库访问不到

    public static String getAddress(String number) {

        String address = "未知号码";//默认
        // 获取数据库对象
        SQLiteDatabase database = SQLiteDatabase.openDatabase(PATH, null,
                SQLiteDatabase.OPEN_READONLY);

        // 数据号码特点:1+(3,4,5,6,7,8)+(1-9)
        // 正则表达式
        // ^1[3-8]\d{9}$
        if (number.matches("^1[3-8]\\d{9}$")) {// 匹配手机号码
            Cursor cursor = database
                    .rawQuery(
                            "select location from data2 where id=(select outkey from data1 where id=?)",
                            new String[] { number.substring(0, 7) });
            if (cursor.moveToNext()) {
                address = cursor.getString(0);
            }
            cursor.close();
        } else if (number.matches("\\d+$")) {
            switch (number.length()) {
            case 3:
                address = "报警电话";
                break;
            case 4:
                address = "模拟器";
                break;
            case 5:
                address = "客服电话";
                break;
            case 8:
                address = "本地电话";
                break;

            default:
                // 010-88881234
                if (number.startsWith("0") && number.length() > 10) {
                    // 有些区号是4位,有些区号是3位
                    Cursor cursor = database.rawQuery(
                            "select location from data2 where area=?",
                            new String[] { number.substring(1, 4) });

                    if (cursor.moveToNext()) {
                        address = cursor.getString(0);
                    } else {
                        cursor = database.rawQuery(
                                "select location from data2 where area=?",
                                new String[] { number.substring(1, 3) });
                        if (cursor.moveToNext()) {
                            address = cursor.getString(0);
                        }
                        cursor.close();
                    }
                    break;
                }
            }

        }
        database.close();//关闭数据库
        return address;
    }

}

4.最后是我们的activity,只有EditText,Buttion,TextView这三个,布局文件略。

public class AddressQueryActivity extends Activity {

    private EditText etQueryPhone;
    private TextView tvQueryResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_address_query);

        etQueryPhone = (EditText) findViewById(R.id.et_queryPhone);
        tvQueryResult = (TextView) findViewById(R.id.tv_queryResult);

        etQueryPhone.addTextChangedListener(new TextWatcher() {//etittext变化监听器,可以不让用户去点击按钮

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String address = AddressDao.getAddress(s.toString());
                tvQueryResult.setText(address);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void query(View view) {
        String phone = etQueryPhone.getText().toString().trim();

        if (!TextUtils.isEmpty(phone)) {
            String address = AddressDao.getAddress(phone);
            tvQueryResult.setText(address);
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值