手機歸屬地查詢

来源:http://www.cnblogs.com/Renyi-Fan/archive/2017/08/30/7451204.html
-Advertisement-
Play Games

手機歸屬地查詢 效果圖: 分析: 1、傳遞多個參數,用一個類就好 2、打開資料庫 private SQLiteDatabase database; database=SQLiteDatabase.openOrCreateDatabase(file, null); file是資料庫的路徑 3、在邏輯中 ...


手機歸屬地查詢

效果圖:

 

分析:

1、傳遞多個參數,用一個類就好

2、打開資料庫

  private SQLiteDatabase database;

  database=SQLiteDatabase.openOrCreateDatabase(file, null);

    file是資料庫的路徑

3、在邏輯中多加判斷

  比如是否獲取到正確的手機號

  比如我們操作的字元串是否為空

  比如時候獲取正確參數

4、通過文件流來實現釋放APK中包中的資料庫文件到手機本地

5、需要用的資料庫放在assets目錄中

  bufferIn = new BufferedInputStream(getAssets().open("naddress.db"));

6、確保輸出流flush,如果不flush,數據會變少,有一部分沒有成功寫出到本地

  bufferOut.flush();

7、try-catch的時候記得finally,去關閉輸入流和輸出流

8、-1為文件末

  while ((len = bufferIn.read(buffer)) != -1)

9、File file = getDatabasePath("naddress.db");

  這個方法得到這樣的路徑data/data/包/database/naddress.db

10、如果文件沒有成功創建,我們取創建文件夾

  if (!file.exists())

    file.getParentFile().mkdirs();

11、正則表達式匹配手機號

  Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");  

  Matcher m = p.matcher(phoneNumber);  

  if (!m.matches()) 

12、百度的正確姿勢

  java 正則表達式驗證手機號

13、找錯誤的時候多去看cause by

 

代碼:

/查詢手機號歸屬地2/res/layout/activity01.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <LinearLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:orientation="horizontal" >
11 
12         <TextView
13             android:layout_width="wrap_content"
14             android:layout_height="wrap_content"
15             android:text="請輸入手機號碼:" />
16 
17         <EditText
18             android:id="@+id/et_mobileNum"
19             android:layout_width="205dip"
20             android:layout_height="wrap_content" />
21     </LinearLayout>
22 
23     <TextView
24         android:id="@+id/tv_city_cardType"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:textColor="#ff0000"
28         android:textSize="25sp"
29         android:text="歸屬地:" />
30 
31     <Button 
32         android:id="@+id/btn_search"
33         android:layout_width="wrap_content"
34         android:layout_height="wrap_content"
35         android:layout_gravity="center_horizontal"
36         android:onClick="query"
37         android:text="查詢號碼歸屬地" />
38 
39 </LinearLayout>
界面設計

database.AddressDao

 1 package database;
 2 
 3 import java.io.File;
 4 
 5 import bean.InfoBean;
 6 import android.database.Cursor;
 7 import android.database.sqlite.SQLiteDatabase;
 8 
 9 /*
10  * 操作資料庫
11  */
12 public class AddressDao {
13     private SQLiteDatabase database;
14     public AddressDao(File file){
15         database=SQLiteDatabase.openOrCreateDatabase(file, null);
16     }
17     /**
18      * 獲取城市及卡類型
19      * @param mobilePrefix 手機號碼首碼
20      */
21     public InfoBean getCityOrCardType(String mobilePrefix){
22         String sql="select city,cardtype from address_tb where _id in(select outkey from numinfo where mobileprefix=?);";
23         Cursor cursor=database.rawQuery(sql, new String[]{mobilePrefix});
24         //就一條記錄,不用用while,就if就好了
25         if(cursor.moveToNext()){
26             String city=cursor.getString(cursor.getColumnIndex("city"));
27             String cardtype=cursor.getString(cursor.getColumnIndex("cardtype"));
28             return new InfoBean(city, cardtype);
29         }
30         return null;
31     }
32 }
資料庫操作

bean.InfoBean

 1 package bean;
 2 
 3 public class InfoBean {
 4     private String city;
 5     private String cardType;
 6     
 7     
 8     public InfoBean(String city, String cardType) {
 9         super();
10         this.city = city;
11         this.cardType = cardType;
12     }
13     public String getCity() {
14         return city;
15     }
16     public void setCity(String city) {
17         this.city = city;
18     }
19     public String getCardType() {
20         return cardType;
21     }
22     public void setCardType(String cardType) {
23         this.cardType = cardType;
24     }
25     
26     
27 }
傳遞數據的類(城市及歸屬地)

fry.Activity01

  1 package fry;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.File;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.util.regex.Matcher;
  9 import java.util.regex.Pattern;
 10 
 11 import bean.InfoBean;
 12 
 13 import com.example.searchMobileCity.R;
 14 
 15 import database.AddressDao;
 16 import android.app.Activity;
 17 import android.os.Bundle;
 18 import android.view.View;
 19 import android.view.View.OnClickListener;
 20 import android.widget.Button;
 21 import android.widget.EditText;
 22 import android.widget.TextView;
 23 import android.widget.Toast;
 24 
 25 public class Activity01 extends Activity {
 26     private Button btn_search;
 27     private EditText et_mobileNum;
 28     private TextView tv_city_cardType;
 29     private AddressDao dao;
 30 
 31     @Override
 32     protected void onCreate(Bundle savedInstanceState) {
 33         // TODO Auto-generated method stub
 34         super.onCreate(savedInstanceState);
 35         setContentView(R.layout.activity01);
 36         initView();
 37         // file就是資料庫文件路徑
 38         File file = initDatabaseData();
 39         dao = new AddressDao(file);
 40 
 41     }
 42 
 43     private void initView() {
 44         tv_city_cardType = (TextView) findViewById(R.id.tv_city_cardType);
 45         et_mobileNum = (EditText) findViewById(R.id.et_mobileNum);
 46         btn_search = (Button) findViewById(R.id.btn_search);
 47     }
 48 
 49     /*
 50      * 查詢歸屬地
 51      */
 52     public void query(View view) {
 53         tv_city_cardType.setText("歸屬地:");
 54         String phoneNumber = et_mobileNum.getText().toString();
 55         Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");  
 56         Matcher m = p.matcher(phoneNumber);  
 57         if (!m.matches()) {
 58             Toast.makeText(this, "請輸入正確的手機號", Toast.LENGTH_SHORT).show();
 59             return ;
 60         } else {
 61             String mobilePrefix = phoneNumber.substring(0, 7);
 62             InfoBean bean = dao.getCityOrCardType(mobilePrefix);
 63             if (bean == null) {
 64                 tv_city_cardType.setText("沒查詢到該號碼的城市與歸屬地!!");
 65             } else {
 66                 tv_city_cardType.setText("城市:" + bean.getCity() + "   \n卡類型: "
 67                         + bean.getCardType());
 68             }
 69         }
 70     }
 71 
 72     /*
 73      * 釋放APK中包中的資料庫文件到手機本地 讀和寫
 74      */
 75     private File initDatabaseData() {
 76         // 這個方法得到這樣的路徑data/data/包/database/naddress.db
 77         File file = getDatabasePath("naddress.db");
 78         if (!file.exists()) {
 79             file.getParentFile().mkdirs();
 80         } else {
 81             return file;
 82         }
 83 
 84         // 獲取讀入流
 85         BufferedInputStream bufferIn = null;
 86         // 輸出流
 87         BufferedOutputStream bufferOut = null;
 88         try {
 89             bufferIn = new BufferedInputStream(getAssets().open("naddress.db"));
 90             bufferOut = new BufferedOutputStream(new FileOutputStream(file));
 91             // 開始釋放緩存流
 92             // 讀的時候,弄個緩存區,讓釋放快一點
 93             byte[] buffer = new byte[8000];
 94             int len = 0;
 95             // -1為文件末
 96             while ((len = bufferIn.read(buffer)) != -1) {
 97                 bufferOut.write(buffer, 0, len);
 98                 // 不flush的話文件大小會變小的
 99                 bufferOut.flush();
100             }
101             return file;
102         } catch (IOException e) {
103             // TODO Auto-generated catch block
104             e.printStackTrace();
105         } finally {
106             try {
107                 if (bufferIn != null)
108                     bufferIn.close();
109                 if (bufferOut != null)
110                     bufferOut.close();
111             } catch (IOException e) {
112                 e.printStackTrace();
113             }
114         }
115         return null;
116     }
117 }
主activity

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 背景 最近在寫一個基於Android的IPC實現的一個小工具,主要實現的就是能夠在手機查看被監視程式的值的變化和日誌等。因為用了入侵的方式,所以需要被監視APK集成一個SDK。程式界面一覽: <! 工程結構以及SDK簡單示例: ! 大概還是一個半成品的樣子,後續會寫一些Root以後才有的功能。 遇到 ...
  • 輔助功能(AccessibilityService)其實是一個Android系統提供給的一種服務,本身是繼承Service類的。這個服務提供了增強的用戶界面,旨在幫助殘障人士或者可能暫時無法與設備充分交互的人們。從開發者的角度看,其實就是提供兩種功能:查找界面元素,實現模擬點擊。實現一個輔助功能服務... ...
  • Android中的隨筆提示文本組件AutoCompleteTextView的使用,此組件用於輸入文本,然後就會在所配置的適配器中的數據進行查找顯示在組件下麵。 ...
  • 1. jar包下載 下載地址:http://ormlite.com/releases/,一般用core和android包即可。 如果使用的是android studio,也可以直接通過module settings加入依賴。 2. 實體類 使用OrmLite創建表不需要寫任何SQL語句,而是通過創建 ...
  • Android中,子線程使用主線程中的組件出現問題的解決方法 ...
  • 資源使用 Android 中支持三種格式的點陣圖文件:.png(首選), .jpg(可接受),.gif(不建議) 為什麼首推 PNG 呢? 官網的描述如下: 註:在構建過程中,可通過 aapt 工具自動優化點陣圖文件,對圖像進行無損壓縮。例如,不需要超過 256 色的真彩色 PNG 可通過調色板轉換為 ...
  • 在開始之前,我們需要創建一個DrawRectView 其初始代碼為 在ViewController中使用(尺寸為100x100並居中) 顯示效果如下(用紅色邊框顯示邊界) 修改DrawRectView.m代碼如下 其實就添加了下麵的繪圖代碼而已,繪製7條線條,每條線條的寬度為0.5 效果如下 將圖片 ...
  • 官方鏈接: https://developer.apple.com/app-store/review/guidelines/cn/ 1.條款和條件 1.1為App Store開發程式,開發者必須遵守Program License Agreement (PLA)、人機交互指南(HIG)以及開發者和蘋果 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...