实现归属地查询大体有两种方法可以实现,一种是通过归属地API进行查询,另一种是查询本地数据库。两种方法各有优劣,最好结合起来使用,我本次采用的是查询数据库的方法。首先需要从网上下载归属地数据库callHomeDB.db。CSDN就可以下载。

  下载好数据库之后,可以利用一个小软件SQLite Expert Professional对数据库进行查看,同时该软件也支持对SQL语句进行操作。

  有了数据库之后就可以进行代码编写了:

  1. 将数据库文件复制到Android Studio工程目录assets下。

  2. 对数据库进行复制,将assets目录下的数据库复制到工程目录下:

     /**
* 复制数据库
*/
private void copyDB(final String dbname){
new Thread(){
public void run(){
try {
File file = new File(getFilesDir(),dbname);
if (file.exists()&&file.length()>0){
Log.i("VirusScanActivity","数据库已存在");
return;
}
InputStream is = getAssets().open(dbname);
FileOutputStream fos = openFileOutput(dbname,MODE_PRIVATE);
byte[] buffer = new byte[1024];
int len = 0;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
is.close();
fos.close();
}catch (Exception e){
e.printStackTrace();
}
}
}.start();
}

  3. 对数据库执行查询语句,获取查询结果

    public static String checkCallHome(String phonenumber){
String desc = null; SQLiteDatabase db = SQLiteDatabase.openDatabase("data/data/com.example.user.callhomedemo/files/callHomeDB.db",null,SQLiteDatabase.OPEN_READONLY);
Cursor cursor = db.rawQuery("select * from mob_location where _id = ?",new String[]{phonenumber.substring(0,7)});
if (cursor.moveToNext()){
desc = cursor.getString(1);
}
cursor.close();
db.close();
return desc;
}

   演示图片如下:

  

最新文章

  1. Linux虚拟化学习笔记<一>
  2. 增强for循环(forearch)
  3. soap ui 进行接口测试
  4. Nginx php-fpm php mysql
  5. The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProv
  6. 通过SEP禁用USB
  7. 【转载】在LoadRunner向远程Linux/Unix执行命令行并收集性能数据
  8. 管理Fragment
  9. HTML5 input新增的几种类型(数字、日期、颜色选取、范围)
  10. PAT-乙级-1041. 考试座位号(15)
  11. [Math]Sqrt(x)
  12. 【Android 多媒体开发】 MediaPlayer 状态机 接口 方法 解析
  13. Android项目---HtmlParse
  14. WebServerice
  15. org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'username' not found. Available parameters are [1, 0, param1, param2]
  16. 分布式监控系统Zabbix3.2对数据库的连接数预警
  17. springMVC框架核心方法调用源码解析
  18. ASP.NET MVC 3 Application Upgrader
  19. 洛谷P3354 河流
  20. Python高级有关的题目

热门文章

  1. 利用background-attachment做视差滚动效果
  2. c++之vector
  3. python - socket - server
  4. Ajax 无刷新上传文件插件 uploadify 的使用
  5. Docker-compose命令详解
  6. buffer pool
  7. css3渐变之linear-gradient与-webkit-linear-gradient写法异同
  8. 有关segue的简介
  9. Mysql新建表,插入中文时报错“Incorrect string value: '\xE4\xBD\xA0\xE5\xA5\xBD' for column”问题
  10. JavaScript 字符 "转换