欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析Android手機衛(wèi)士之號碼歸屬地查詢

 更新時間:2016年04月22日 10:53:55   作者:陶士涵  
這篇文章主要介紹了淺析Android手機衛(wèi)士之號碼歸屬地查詢的相關資料,需要的朋友可以參考下

使用小米號碼歸屬地數(shù)據(jù)庫,有兩張表data1和data2

先查詢data1表,把手機號碼截取前7位

select outkey from data1 where id=”前七位手機號”

再查詢data2表,

select location from data2 where id=”上面查出的outkey”

可以使用子查詢

select location from data2 where id=(select outkey from data1 where id=”前7位手機號”)

創(chuàng)建數(shù)據(jù)庫工具類

新建一個包xxx.db.dao

新建一個類NumberAddressUtils,新建一個靜態(tài)方法queryNumber

調(diào)用SQLiteDatabase.openDatabase()方法,獲取到SQLiteDatabase對象,參數(shù):數(shù)據(jù)庫路徑(/data/data/包名/files/xxx.db),游標工廠(null),打開方式(SQLiteDatabse.OPEN_READONLY)

把數(shù)據(jù)庫address.db拷貝到 /data/data/包名/files/目錄里面

調(diào)用SQLiteDatabase對象的rawQuery()方法,獲取到Cursor對象,查詢數(shù)據(jù),參數(shù):sql語句,string[]條件數(shù)組

例如:select location from data2 where id=(select outkey from data1 where id=?) ,new String[]{phone.subString(0,7)}

while循環(huán)Cursor對象,條件調(diào)用Cursor對象的moveToNext()方法

循環(huán)中調(diào)用Cursor對象的getString()方法,傳入字段索引

關閉游標Cursor對象的close()方法

把得到的地址返回出去

拷貝數(shù)據(jù)庫從assets目錄到data目錄

在歡迎頁面,進行拷貝

調(diào)用getAssets().open()方法,得到InputStream對象,參數(shù):xxx.db文件名

獲取File對象,new出來,參數(shù):getFilesDir()獲取到/data/data/包/files/目錄,xxx.db

獲取FileOutputStream對象,new出來,參數(shù):File對象

定義緩沖區(qū)byte[] buffer,一般1024

定義長度len

while循環(huán)讀取,條件:讀入的長度不為-1

循環(huán)中調(diào)用FileOutputStream對象的write()方法,參數(shù):緩沖區(qū),從0開始,len長度

調(diào)用InputStream對象的close()方法

判斷只要存在和長度大于0就不再拷貝了,調(diào)用File對象的exist()方法和length()方法大于0

NumberQueryAddressUtil.java

package com.qingguow.mobilesafe.utils;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class NumberQueryAddressUtil {
private static final String path = "/data/data/com.qingguow.mobilesafe/files/address.db";
/**
* 查詢號碼歸屬地
* @param phone
* @return
*/
public static String queryAddress(String phone){
SQLiteDatabase db=SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READONLY);
Cursor cursor=db.rawQuery("select location from data2 where id=(select outkey from data1 where id=?)", new String[]{phone.substring(0,7)});
while(cursor.moveToNext()){
String address=cursor.getString(0);
return address;
}
cursor.close();
return "";
}
} 

拷貝數(shù)據(jù)庫

private void copyAddressDatabase() {
try {
//判斷是否存在
File file = new File(getFilesDir(), "address.db");
if (file.exists() && file.length() > 0) {
return;
}
InputStream is = getAssets().open("address.db");
FileOutputStream fos = new FileOutputStream(file);
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();
}
}

推薦閱讀:

淺析Android手機衛(wèi)士sim卡綁定

深入淺析Android手機衛(wèi)士保存密碼時進行md5加密

詳解Android 手機衛(wèi)士設置向?qū)ы撁?/a>

淺析Android手機衛(wèi)士關閉自動更新

淺析Android手機衛(wèi)士自定義控件的屬性

淺析Android手機衛(wèi)士讀取聯(lián)系人

淺析Android手機衛(wèi)士接收短信指令執(zhí)行相應操作

淺析Android手機衛(wèi)士手機定位的原理

淺析Android手機衛(wèi)士之手機實現(xiàn)短信指令獲取位置

以上內(nèi)容是小編給大家介紹的Android手機衛(wèi)士之號碼歸屬地查詢的相關內(nèi)容,希望對大家有所幫助!

相關文章

最新評論