This part was learned yesterday, but because the eye overload so drag to the present, after the reasonable planning time.
At present, the number of the phone inside the query is mainly in two ways: 1. Networked queries, 2. Match the native attribution database.
I think the best combination of the two, in the local database matching in the network query, can greatly increase the matching effect, and do not have to increase the capacity of the local database to increase the size of the installation package.
Step: 1. When the software is turned on, copy the database from the assets directory to the files directory, and if it already exists, do not copy it again.
2. Implement the interface.
3. Implement the Getphoneaddress () method of the tool class Phoneaddressutils
4. Invoke the tool class method in the interface class activity to get the address displayed.
The first thing to do is copy the database:
private void Copydb () {File File = new file (Getfilesdir (), "address.db"), if (File.exists () &&file.length () >0) {Toast.maketext (this, "replicated database", 0). Show (); 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);}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Because the running tool class does not get the files in the assets directory, copy the database to the files directory in the boot-up splashactivity.
To get the file in the Assert directory, open the file to return the input stream using the Assetmanager object's open () method.
Implementation interface: Just a simple input box, button, display box on the line.
Tool class:
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 ";p ublic 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 data1 where I D=?) "; Sqlitedatabase database = sqlitedatabase.opendatabase (path, NULL, sqlitedatabase.open_readonly); cursor cursor = database.rawquery (sql, new string[] {number.substring (0, 7)}); while (Cursor.movetonext ()) {address = Cursor.getstring (0);} Cursor.close ();d atabase.close ();} else {address = "not a mobile number";} return address;}}
Call Display:
When you click the button:
public void queryaddress (view view) {String number = Et_phone.gettext (). toString (); String address = addressdao.getaddress (number); Et_address.settext (address);}
But we want to display the position dynamically, so we want to add a textchangedlistener to the input box, and when the input string is greater than 3, the automatic call is displayed.
Et_phone.addtextchangedlistener (New Textwatcher () {@Overridepublic void ontextchanged (charsequence s, int start, int before, int count) {//TODO auto-generated method Stubif (S.length () >3) {String address = addressdao.getaddress (s.tostr ing ()); Et_address.settext (address);}} @Overridepublic void Beforetextchanged (charsequence s, int start, int count,int after) {//TODO auto-generated method stub } @Overridepublic void aftertextchanged (Editable s) {//TODO auto-generated Method stub}});