Android中利用数据库查询电话归属地
2015-12-15 23:28
543 查看
1.首先将我们的归属地的数据库放入assets/address.db
2.然后需要将我们的数据库文件copy到
data/data/com.ittest.address/files/address.db
3.创建一个AddressDao.java,进行数据库查询操作
手机匹配较为简略,但是大致能够覆盖,但是对于3位,4位号码由于缺少数据库的支持,所以只是写个大概,知道意思就行了。
4.最后是我们的activity,只有EditText,Buttion,TextView这三个,布局文件略。
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); } } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories