Mobile Security Defender------Query number attribution

Source: Internet
Author: User


Technical points :

    • Query operations for databases
    • Regular expressions
    • Monitoring input events for input boxes

Ideas :

There are two ways to query the attribution of a number:
1. Network query, through some of the Internet interface, you can query to JSON data, etc.

2. Local query, APK, bring a database, place number segment corresponding location information

Considering the user's usage scenario, we decided to implement this function by local query.

    • This feature uses the database provided by Xiaomi.
    • There are two tables inside:
    • Data1:id: First seven digits of mobile number Outkey: foreign key
    • Data2:id: Corresponding to the foreign key in table 1 location: Position Area: Code

How to find your phone number :

First, the corresponding foreign key is queried:
Select Outkey from data1 where id = "First seven digits of the number"

The location is then queried based on the foreign key
Select location from data2 where id = "FOREIGN key value"

That
Execute SQL statement:
Select location from data2 where id = (select Outkey from data1 where id = "First seven digits of the number")
Note: When you nest another SQL statement in one SQL statement, enclose it in parentheses.

To inquire about fixed-line phone ideas:

Determine if the number starts with 0 and is longer than 10
If it is, then intercept the number of the 第2-3位, the database query;
If not, the intercept number of the 2-4-bit database query;
If not, return the number directly.

Select location from data2 where area= "intercept that part of the number"

Also, the data is "formatted" with regular expressions
1) Start with ^
2) [XYZ] determines that the location of the data must be one of the x/y/z
3) [X-z] indicates that the value of the position is x-z
4) \d = 0-9 digits
5) {n} shows how many times
6) End With $

Then, the cell phone number of the regular expression:
^1[3458]\d{9}$

Copy data to the phone

The database is placed in the raw folder and copied to the phone's memory when the splash page runs. Specific operation:

 Private void Copydatabasetomobile()    {//1. Converting raw files to byte streamsInputStream is= Splashactivity. This. Getresources (). Openrawresource (r.raw.address);//2. Copying data into memoryFile File =NewFile (Querynumberutils.database_path);Try{FileOutputStream FOS =NewFileOutputStream (file);byte[] buffer =New  byte[1024x768];intLength =-1; while(length = is. Read (buffer))! =-1) {fos.write (buffer,0, length); } fos.close (); is. Close (); }Catch(IOException e)        {E.printstacktrace (); }    }

Let the result text change as the contents of the Input box change:

The Addtextchangedlistener () method, an example of a parameter of Textwacher, is created as an anonymous inner class because Textwatcher is an interface, and overrides three of these methods beforetextchanged, OnTextChanged, aftertextchanged

Among them, the main rewrite ontextchanged, let it after the callback to perform the query address operation.

The final implementation code:

Metnumber.addtextchangedlistener (NewTextwatcher () {@Override     Public void beforetextchanged(Charsequence S,intStartintCountintAfter) {}//actions that occur when text content changes     Public void ontextchanged(Charsequence S,intStartintBefore,intCount) {if(s! =NULL&& s.length () >=3) {Mtvresult.settext (Querynumberutils.getlocationbynumber (s.tostring ())); }if(S.length () <=2) {Mtvresult.settext (""); }     }@Override         Public void aftertextchanged(Editable s) {}});

Code for the query function:

 Public StaticStringGetlocationbynumber(String number) {String location =NULL;//Determine if the format is a mobile phone numberString regular ="^1[34568]\\d{9}$";if(Number.matches (Regular)) {//Is mobile phone numberLocation = Queryphonenumber (number); }Else{location = Queryothernumber (number); }returnLocation }//inquire about the operation of the mobile phone number attribution Place    Private StaticStringQueryphonenumber(String number) {Sqlitedatabase SDB = sqlitedatabase.opendatabase (Database_path,NULL, sqlitedatabase.open_readonly); String location =NULL; cursor cursor = Sdb.rawquery ("Select location from data2 where id = (select Outkey from data1 where id =?)",NewString[]{number.substring (0,7)}); while(Cursor.movetonext ()) {location = Cursor.getstring (0); }if(Location = =NULL{location = number; } cursor.close ();returnLocation }//Query The operation of the other number's place of attribution    Private StaticStringQueryothernumber(String number) {Sqlitedatabase SDB = sqlitedatabase.opendatabase (Database_path,NULL, sqlitedatabase.open_readonly); String location =NULL;Switch(Number.length ()) { Case 3://similar to 110 119 120 special PhonesLocation ="Special Phone"; Break; Case 4: location ="Simulator Phone"; Break; Case 5://10010 10086 Customer Service PhoneLocation ="Customer Service Phone"; Break;default:if(Number.length () >Ten&& Number.startswith ("0"))                {//This is a fixed phone number                    //Query fixed phone number:Location = Querytelnumber (number); } Break; }if(Location = =NULL{location = number; }returnLocation }//Check the number of the fixed phone to attribution    Private StaticStringQuerytelnumber(String number) {Sqlitedatabase SDB = sqlitedatabase.opendatabase (Database_path,NULL, sqlitedatabase.open_readonly); String location =NULL; cursor cursor =NULL; cursor = Sdb.rawquery ("Select location from Data2 where area=?",NewString[]{number.substring (1,3)}); while(Cursor.movetonext ()) {location = Cursor.getstring (0); } cursor = Sdb.rawquery ("Select location from Data2 where area=?",NewString[]{number.substring (1,4)}); while(Cursor.movetonext ()) {location = Cursor.getstring (0); }if(Cursor! =NULL) {cursor.close ();returnLocation.substring (0, Location.length ()-2); } cursor.close ();returnNumber }

Copyright notice: Just out of the original content of the pot, I hope you have help ~

Mobile Security Defender------Query number attribution

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.