Android实现归属地查询功能
2024-10-12 00:11:22
实现归属地查询大体有两种方法可以实现,一种是通过归属地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;
}
演示图片如下:
最新文章
- Linux虚拟化学习笔记<;一>;
- 增强for循环(forearch)
- soap ui 进行接口测试
- Nginx php-fpm php mysql
- The content of element type ";configuration"; must match ";(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProv
- 通过SEP禁用USB
- 【转载】在LoadRunner向远程Linux/Unix执行命令行并收集性能数据
- 管理Fragment
- HTML5 input新增的几种类型(数字、日期、颜色选取、范围)
- PAT-乙级-1041. 考试座位号(15)
- [Math]Sqrt(x)
- 【Android 多媒体开发】 MediaPlayer 状态机 接口 方法 解析
- Android项目---HtmlParse
- WebServerice
- org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter &#39;username&#39; not found. Available parameters are [1, 0, param1, param2]
- 分布式监控系统Zabbix3.2对数据库的连接数预警
- springMVC框架核心方法调用源码解析
- ASP.NET MVC 3 Application Upgrader
- 洛谷P3354 河流
- Python高级有关的题目
热门文章
- 利用background-attachment做视差滚动效果
- c++之vector
- python - socket - server
- Ajax 无刷新上传文件插件 uploadify 的使用
- Docker-compose命令详解
- buffer pool
- css3渐变之linear-gradient与-webkit-linear-gradient写法异同
- 有关segue的简介
- Mysql新建表,插入中文时报错“Incorrect string value: &#39;\xE4\xBD\xA0\xE5\xA5\xBD&#39; for column”问题
- JavaScript 字符 &;quot;转换