隨著被騙手機數量的日益猖獗,號碼歸屬的顯示也變得越來越重要,武林技術頻道今天就為大家帶來淺析Android手機衛士之號碼歸屬地查詢,希望能幫助到您。
使用小米號碼歸屬地數據庫,有兩張表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位手機號”)
創建數據庫工具類
新建一個包xxx.db.dao
新建一個類NumberAddressUtils,新建一個靜態方法queryNumber
調用SQLiteDatabase.openDatabase()方法,獲取到SQLiteDatabase對象,參數:數據庫路徑(/data/data/包名/files/xxx.db),游標工廠(null),打開方式(SQLiteDatabse.OPEN_READONLY)
把數據庫address.db拷貝到 /data/data/包名/files/目錄里面
調用SQLiteDatabase對象的rawQuery()方法,獲取到Cursor對象,查詢數據,參數:sql語句,string[]條件數組
例如:select location from data2 where id=(select outkey from data1 where id=?) ,new String[]{phone.subString(0,7)}
while循環Cursor對象,條件調用Cursor對象的moveToNext()方法
循環中調用Cursor對象的getString()方法,傳入字段索引
關閉游標Cursor對象的close()方法
把得到的地址返回出去
拷貝數據庫從assets目錄到data目錄
在歡迎頁面,進行拷貝
調用getAssets().open()方法,得到InputStream對象,參數:xxx.db文件名
獲取File對象,new出來,參數:getFilesDir()獲取到/data/data/包/files/目錄,xxx.db
獲取FileOutputStream對象,new出來,參數:File對象
定義緩沖區byte[] buffer,一般1024
定義長度len
while循環讀取,條件:讀入的長度不為-1
循環中調用FileOutputStream對象的write()方法,參數:緩沖區,從0開始,len長度
調用InputStream對象的close()方法
判斷只要存在和長度大于0就不再拷貝了,調用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 "";}} 拷貝數據庫
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手機衛士之號碼歸屬地查詢,希望閱讀完本文以后對你有幫助哦,網絡的普及給大家帶來了很學習的機會。
新聞熱點
疑難解答