First, the goal
1, processing 110, 10086, landline and other non-mobile phone number of the place of attribution;
Second, the Code implementation
1, determine whether the text in the input box conforms to the format of the mobile phone number (1, the second is 345678, followed by 9 digits are 0-9 integers), according to the characteristics of the mobile phone number to determine its regular expression "1[345678]\d{9}", in the code is the expression "^" beginning with "$", At the same time, "\" needs to be escaped, so the condition is "^1[345678]\\d{9}$". The preceding regular expression description: [345678] means that "[]" inside the number of any one, \d represents 0-9 of the number of any one, {9} indicates that the preceding \d exactly match 9 times.
2. In the query number Querynumber (string number) method of the Query tool Class (Numberaddqueryutils) for the numbers attribution, the incoming number is passed through its matches before querying the database (string RegularExpression) method
Determine whether to match the mobile phone number of the regular expression, such as matching query database, if not match query another database (such as 110, 10086, landline, etc.);
Regular expressions for mobile numbers:
1 number.matches ("^1[345678]\\d{9}$")
View Code
3, through the number startswith (String prefix) method to determine whether the number starts with "0", if set up the first 4-digit intercept number of the query Data2 area to obtain the place of attribution, and then the place of attribution of the last two (telecommunications, Unicom, Mobile) to get the city name, and finally return this value, in addition to deal with 010, 021 and other 3-digit area code of special examples;
Code to process a landline:
1 //Processing Landline2 if(Number.startswith ("010")){3Location = "Beijing";4}Else if(Number.startswith ("021")) {5Location = "Shanghai";6}Else if(Number.startswith ("0")){7cursor cursor =Database.rawquery (8"Select location from data2 where area =?",9 NewString[] {number.substring (1, 4) }); Ten while(Cursor.movetonext ()) { OneLocation = cursor.getstring (0). substring (0, cursor.getstring (0). Length ()-2); A } - cursor.close (); -}
View Code
Android instance-Mobile security Defender (34)-Optimize your attribution query