手機歸屬地查詢,

來源:互聯網
上載者:User

手機歸屬地查詢,
手機歸屬地查詢

 

分析:

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、Regex匹配手機號

  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 Regex驗證手機號

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         <TextView13             android:layout_width="wrap_content"14             android:layout_height="wrap_content"15             android:text="請輸入手機號碼:" />16 17         <EditText18             android:id="@+id/et_mobileNum"19             android:layout_width="205dip"20             android:layout_height="wrap_content" />21     </LinearLayout>22 23     <TextView24         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 block104             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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.