Mobile phone home location query,

Source: Internet
Author: User

Mobile phone home location query,
Mobile phone home location query

:

 

Analysis:

1. Pass multiple parameters and use a class.

2. Open the database

Private SQLiteDatabase database;

Database = SQLiteDatabase. openOrCreateDatabase (file, null );

File is the path of the database.

3. More judgment in logic

For example, whether the correct mobile phone number is obtained

For example, whether the string we operate is null

For example, obtain the correct parameters.

4. Release the database files in the APK package to the mobile phone through the file stream

5. Place the databases to be used in the assets Directory.

BufferIn = new BufferedInputStream (GetAssets (). open("Naddress. db "));

6. Make sure that the output stream is flush. If the output stream is not flush, the data will decrease, and some of the data will not be successfully written locally.

BufferOut. flush ();

7. Remember finally when trying-catch to close the input stream and output stream.

8.-1 indicates the end of the file.

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

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

This method is used to obtain the path data/package/database/naddress. db.

10. If the file is not successfully created, we will create a folder.

If (! File. exists ())

File. getParentFile (). mkdirs ();

11. Regular Expression matching mobile phone number

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

Matcher m = p. matcher (phoneNumber );

If (! M. matches ())

12. Correct Baidu posture

Java Regular Expression verification mobile phone number

13. Check cause by when looking for errors.

 

Code:

/Query the mobile phone number of the Region 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 = "Enter your mobile phone number: "/> 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>
Interface Design

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 * Operating database 11 */12 public class AddressDao {13 private SQLiteDatabase database; 14 public AddressDao (File file) {15 database = SQLiteDatabase. openOrCreateDatabase (file, null); 16} 17/** 18 * Get city and card type 19 * @ param mobilePrefix mobile phone number prefix 20 */21 public In FoBean 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 // a record. if you don't need to use while, then 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}
Database Operations

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 }
Data transmission class (city and region)

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 Droid. 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 d Ao; 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 is the database File path 38 file = initDatabaseData (); 39 dao = new AddressDao (file ); 40 41} 42 43 private void initView () {44 TV _city_cardType = (TextView) findViewById (R. id. TV _city_cardTyp E); 45 et_mobileNum = (EditText) findViewById (R. id. et_mobileNum); 46 btn_search = (Button) findViewById (R. id. btn_search); 47} 48 49/* 50 * query destination location 51 */52 public void query (View view) {53 TV _city_cardType.setText ("Destination location :"); 54 String phoneNumber = et_mobileNum.getText (). toString (); 55 Pattern p = Pattern. compile ("^ (13 [0-9]) | (15 [^ 4, \ D]) | (18 [-9]) \ d {8} $ "); 56 Matcher m = p. matcher (phoneNumber); 57 if (! M. matches () {58 Toast. makeText (this, "enter the correct mobile phone number", 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 ("the city and region where the number is not found !! "); 65} else {66 TV _city_cardType.setText (" City: "+ bean. getCity () + "\ n card type:" 67 + bean. getCardType (); 68} 69} 70} 71 72/* 73 * release the database files in the APK package to the mobile phone to read and write 74 */75 private File initDatabaseData () {76 // This method obtains the path data/package/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 // get read stream 85 BufferedInputStream bufferIn = null; 86 // output stream 87 BufferedOutputStream bufferOut = null; 88 try {89 bufferIn = new BufferedInputStream (getAssets (). open ("naddress. db "); 90 bufferOut = new BufferedOutputStream (new FileOutputStream (file); 91 // get a cache zone when starting to release the cache stream 92 // read, release a little faster 93 byte [] buffer = new Byte [8000]; 94 int len = 0; 95 //-1 is the last 96 while (len = bufferIn. read (buffer ))! =-1) {97 bufferOut. write (buffer, 0, len); 98 // if it is not flush, the file size will become 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}
Main activity

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.