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

来源:互联网 发布:淘宝韩国模特名字大全 编辑:程序博客网 时间:2024/04/27 23:50

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);        }    }}
0 0
原创粉丝点击