手机安全卫士------查询号码归属地
效果圖:
技術點:
- 數據庫的查詢操作
- 正則表達式
- 監聽輸入框的輸入事件
思路:
查詢號碼歸屬地的方法有兩種:
1.網絡查詢,通過網上的一些接口,可以查詢到JSON數據等
2.本地查詢,APK中,自帶一個數據庫,放置號段對應的地點信息
考慮到用戶的使用場景,決定采用本地查詢的方式來實現這個功能。
- 本功能采用的是 小米提供的數據庫。
- 內部有兩張表:
- data1: id:手機號碼的前七位 outkey:外鍵
- data2: id:對應表1中的外鍵 location:位置 area:區號
查詢手機號碼的思路:
首先查詢出對應的外鍵:
select outkey from data1 where id = “號碼的前七位”
之后根據外鍵查詢位置
select location from data2 where id = “外鍵的值”
即:
執行SQL語句:
select location from data2 where id = (select outkey from data1 where id = “號碼的前七位” )
注意:當一個SQL語句中嵌套另一個SQL語句的時候,要用括號括起來。
查詢固定電話的思路:
判斷號碼是否以0開頭,且長度大于10
如果是,則截取號碼的第2-3位,進行數據庫查詢;
如果沒有,則截取號碼的2-4位進行數據庫查詢;
如果還沒有,那就直接返回號碼
select location from data2 where area=”截取的那部分數字”
同時,還要通過正則表達式對數據進行“格式化”
1) 以^開頭
2)[xyz] 確定該位置的數據一定為x/y/z中的一個
3)[x-z]表示該位置的值為x-z
4)\d 表示0-9數字
5){n} 顯示多少次
6)以$ 結尾
則,手機號的正則表達式:
^1[3458]\d{9}$
復制數據到手機中
數據庫放在raw文件夾內,在Splash頁面運行的時候,復制到手機內存中。具體操作:
private void copyDatabaseToMobile(){//1.把raw文件轉換成字節流InputStream is = SplashActivity.this.getResources().openRawResource(R.raw.address);//2.復制數據到內存中File file = new File(QueryNumberUtils.DATABASE_PATH);try{FileOutputStream fos = new FileOutputStream(file);byte[] buffer = new byte[1024];int length = -1;while((length = is.read(buffer)) != -1){fos.write(buffer,0,length);}fos.close();is.close();}catch (IOException e){e.printStackTrace();}}讓”結果文本”隨著輸入框內容的改變而改變:
addTextChangedListener() 方法,參數為 TextWacher的示例,由于TextWatcher是接口,所以以匿名內部類的形式創建,并重寫其中的三個方法beforeTextChanged、onTextChanged、afterTextChanged
其中,主要重寫onTextChanged,讓它在回調的之后執行查詢地址操作。
最終實現代碼:
mEtNumber.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}//文字內容發生變化時進行的操作public void onTextChanged(CharSequence s, int start, int before, int count) {if(s != null && s.length() >= 3){ mTvResult.setText(QueryNumberUtils.getLocationByNumber(s.toString()));}if(s.length() <= 2){mTvResult.setText("");}}@Overridepublic void afterTextChanged(Editable s) {} });查詢功能的代碼:
public static String getLocationByNumber(String number){String location = null;//判斷格式是否為手機號碼String regular = "^1[34568]\\d{9}$";if(number.matches(regular)){//是手機號碼location = queryPhoneNumber(number);}else{location = queryOtherNumber(number);}return location;}//查詢手機號碼歸屬地的操作private static String queryPhoneNumber(String number){SQLiteDatabase sdb = SQLiteDatabase.openDatabase(DATABASE_PATH,null,SQLiteDatabase.OPEN_READONLY);String location = null;Cursor cursor = sdb.rawQuery("select location from data2 where id = (select outkey from data1 where id = ?)",new String[]{number.substring(0,7)});while(cursor.moveToNext()){location = cursor.getString(0);}if(location == null){location = number;}cursor.close();return location;}//查詢其他號碼歸屬地的操作private static String queryOtherNumber(String number){SQLiteDatabase sdb = SQLiteDatabase.openDatabase(DATABASE_PATH,null,SQLiteDatabase.OPEN_READONLY);String location = null;switch(number.length()){case 3://類似110 119 120之類的特殊電話location = "特殊電話";break;case 4:location = "模擬器電話";break;case 5://10010 10086之類的客服電話location = "客服電話";break;default:if(number.length() > 10 && number.startsWith("0")){//此時為固定電話號碼//查詢固定電話號碼:location = queryTelNumber(number);}break;}if(location == null){location = number;}return location;}//查詢固定電話的號碼歸屬地private static String queryTelNumber(String number){SQLiteDatabase sdb = SQLiteDatabase.openDatabase(DATABASE_PATH,null,SQLiteDatabase.OPEN_READONLY);String location = null;Cursor cursor = null;cursor = sdb.rawQuery("select location from data2 where area= ?",new String[]{number.substring(1,3)});while(cursor.moveToNext()){location = cursor.getString(0);}cursor = sdb.rawQuery("select location from data2 where area= ?",new String[]{number.substring(1,4)});while(cursor.moveToNext()){location = cursor.getString(0);}if(cursor != null) {cursor.close();return location.substring(0, location.length() - 2);}cursor.close();return number;}總結
以上是生活随笔為你收集整理的手机安全卫士------查询号码归属地的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 「镁客·请讲」VR的拓荒者,幻境视界让V
- 下一篇: 20万奖补|成都两化融合管理贯标项目拟支