Android mobile phone number attribution query implementation
This part was learned yesterday, but because of the overload of eyes, we need to plan the time in the future.
Currently, the number attribution query in the mobile phone is mainly in two ways: 1. online query, 2. Match the local number attribution database.
I think it is best to combine the two methods. online queries that cannot be matched in the local database can greatly increase the matching effect without increasing the local database capacity and increasing the installation package size.
Step: 1. When the software is enabled, copy the database from the assets Directory to the files directory. If the database already exists, do not copy it again.
2. Implementation interface.
3. Implement the getPhoneAddress () method of the tool class PhoneAddressUtils
4. Call the tool class method in the interface class activity to get the address display.
First, copy the database:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Private void copyDB (){ File file = new File (getFilesDir (), "address. db "); If (file. exists () & file. length ()> 0 ){ Toast. makeText (this, "Copied Database", 0). show (); } Else { Try { AssetManager am = getAssets (); Byte [] buffer = new byte [1024]; InputStream is = am. open ("address. db "); FileOutputStream FCM = new FileOutputStream (file ); Int len = 0; While (len = is. read (buffer)> 0 ){ FS. write (buffer, 0, len ); } } Catch (IOException e ){ // TODO Auto-generated catch block E. printStackTrace (); } } } |
Because the files in the assets Directory are not available in the tool class during running, copy the database to the files directory in the SplashActivity started at startup.
To obtain files under the assert directory, use the open () method of the AssetManager object to open the file and return the input stream.
Implementation interface: A simple input box and button are displayed.
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 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/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 data1 where id = ?) "; 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 (); Database. close (); } Else { Address = "not a mobile phone number "; } Return address; } } |
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 ); } |
However, we need to dynamically display the position, so we need to add a TextChangedListener to the input box. When the input string is greater than 3, it will be automatically called for matching and 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, Int after ){ // TODO Auto-generated method stub } @ Override Public void afterTextChanged (Editable s ){ // TODO Auto-generated method stub } }); |