Android mobile phone number attribution to the query implementation

Source: Internet
Author: User
Tags stub tostring

This part was learned yesterday, but because of the eye overload so dragged to the present, the future should be reasonable planning time.

At present, the number of the phone in the home query is mainly through two ways: 1 network query, 2. Match the local database of ownership.

I think the two kinds of combination is the best, in the local database can not match the network query, to greatly increase the matching effect, and do not have to increase the local database capacity and increase the size of the installation package.

Steps: 1. When the software is turned on, copy the database from the assets directory to the files directory, if it already exists, you do not have to copy it again.

2. Implement the interface.

3. Getphoneaddress () method to implement tool class Phoneaddressutils

4. Invoke the tool class method in the interface class activity to get the address soldier displayed.

The first is to copy the operation of the database:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 private void Copydb () {    file file = new file (Getfi Lesdir (), "address.db");     if (File.exists () &&file.length () >0) {         toast.maketext (This, "replicated database", 0). Show ();    &NBSP} else {        try {             assetmanager am = getassets ();                            byte[] buffer = new byte[1024];             inputstream is = Am.open ("address.db");             fileoutputsTream fis = new FileOutputStream (file);             int len = 0;             while ((len=is.read (buffer)) >0) {                 fis.write ( Buffer, 0, Len);             }         &NBSP} catch (IOException e) {            // TODO auto-generated Catch block              E.printstacktrace ();        &NBSP}     }             }


Because the Run-time tool class does not get the files in the assets directory, copy the database to the files directory in the boot splashactivity.

To get the file in the Assert directory to use the Assetmanager object's open () method, open the file to return the input stream.

Implementation interface: Just a simple input box, button, display box on the line.

Tool class:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 The 25 26 package Com.itheima.mobilesafe.db.dao;   Import android.database.Cursor; Import Android.database.sqlite.SQLiteDatabase;   public class Addressdao {    private static String path = "data/data/com.itheima.mobilesafe/ Files/address.db ";     public static string GetAddress (string number) {                   string address = number;         if (Number.matches ("^1[34568]d{9}$")) {             string sql = "Select location from data2 where id = (select Outkey from Da Ta1 where id=?) ";                 sqlitedatabase Database = sqlitedatabase.opendatabase (path, NULL, sqlitedatabase.open_readonly);           &Nbsp; cursor Cursor = database.rawquery (sql, new string[] {number.substring (0, 7)});             while (Cursor.moveToNext ()) {                 address = Cursor.getstring (0);             }              cursor.close ();             database.close ();        &NBSP} else {             address = "Not cell phone number";         }                    return address;    &NBSP}}

Call Display:

When you click the button:

?
1 2 3 4 5 public void queryaddress (view view) {String number = Et_phone.gettext (). toString ();         String address = addressdao.getaddress (number);     Et_address.settext (address); }

But we want to dynamically display the position, so to add a textchangedlistener to the input box, when the input string is greater than 3, the automatic call to match the display.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23-24 Et_phone.addtextchangedlistener (New Textwatcher () {                            @Override              public void OnTextChanged (CharSequence s, int Start, int before, int count) {                 //TODO auto-generated Method Stub                  if (S.length () >3) {                     string address = addressdao.getaddress (s.toString ());                      et_address.settext (address);                 }             }                             @Override              public void Beforetextchanged (charsequence s, int start, int count,      & Nbsp;              int after) {                 //TODO Auto-generated Method Stub                                }                              @Override             public void AfterTextChanged ( Editable s) {                // TODO auto-generated Method Stub                                }          });
Related Article

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.