Since it is a stand-alone version, it is necessary to query the local database, so we have to prepare an offline database file (: http://download.csdn.net/detail/rowandjj/7660979).steps:
1. Create a tool class to open the database:
Package Cn.edu.chd.mobilesafe.db.dao;import Android.database.sqlite.sqlitedatabase;public Class AddressDao{public Static Sqlitedatabase Getaddressdb (String path) {return sqlitedatabase.opendatabase (path, NULL, Sqlitedatabase.open_ READONLY);}}
2. Writing Business methods:You first need to use regular expressions to determine whether the number is a mobile phone number or a fixed phone. If the phone number is based on the previous 7-bit query database. If a fixed telephone, the first to determine the length of the fixed phone, divided into the following situations: 3-bit area code + 7-digit 3-bit area code + 8-digit 4-bit area code + 7-digit 4-bit area code + 8-digit number according to the area code to find the place of attribution
Package Cn.edu.chd.mobilesafe.engine;import Android.database.cursor;import Android.database.sqlite.SQLiteDatabase ; Import Android.os.environment;import Cn.edu.chd.mobilesafe.db.dao.addressdao;public class AddressService{public static string GetAddress (string number) {String city = number; Sqlitedatabase db = Addressdao.getaddressdb (Environment.getexternalstoragedirectory (). GetPath () + "/address.db"); if (Number.matches ("^1[3458]\\d{9}$"))//mobile number {if (Db.isopen ()) {cursor cursor = db.rawquery ("Select City from info where Mobileprefix =? ", new String[]{number.substring (0, 7)}), if (Cursor.movetonext ()) {city = cursor.getstring (0);}} Db.close ();} else//fixed phone {int len = number.length (); switch (len) {case 4:city = "simulator"; break;case 7:case 8:city = "local number"; Break;case 10://3 bit Area code + 7-digit number if (Db.isopen ()) {cursor cursor = db.rawquery ("Select city from info where area =?") Limit 1 ", New String[]{number.substring (0, 3)}), if (Cursor.movetonext ()) {city = cursor.getstring (0);} Db.close ();} Break;case 11://3 Area code + 8-digit number, 4-bit area code + 7-digit number if (db).IsOpen ()) {cursor cursor = db.rawquery ("Select city from info where area =?") Limit 1 ", New String[]{number.substring (0, 3)}), if (Cursor.movetonext ()) {city = cursor.getstring (0);} cursor = db.rawquery ("Select city from info where area =?") Limit 1 ", New String[]{number.substring (0, 4)}), if (Cursor.movetonext ()) {city = cursor.getstring (0);} Db.close ();} Break;case 12:if (Db.isopen ()) {cursor cursor = db.rawquery ("Select city from info where area =?") Limit 1 ", New String[]{number.substring (0, 4)}), if (Cursor.movetonext ()) {city = cursor.getstring (0);} Db.close ();} Break;}} if (Db.isopen ()) {db.close ();} return city;}}
3. Call the business class and inquire about the phone's attribution information:
because it is a database operation, Asynctask is used for asynchronous queries.
Package Cn.edu.chd.mobilesafe.ui;import Android.app.activity;import Android.os.asynctask;import android.os.Bundle; Import Android.view.view;import Android.view.view.onclicklistener;import Android.view.animation.animation;import Android.view.animation.animationutils;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;import Android.widget.toast;import Cn.edu.chd.mobilesafe.r;import Cn.edu.chd.mobilesafe.engine.addressservice;public class Querynumberactivity extends Activity{private Button but_ query = null;private EditText et_number = null;private TextView tv_show = null; @Overrideprotected void OnCreate (Bundle sav Edinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.query_number); but_query = (Button) Findviewbyid (r.id.but_query_p); et_number = (EditText) Findviewbyid (r.id.et_query_p); tv_show = (TextView) Findviewbyid (r.id.tv_show_p); But_query.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View V) {String text = Et_number.gettext (). toString (), if (Text.trim (). Equals ("")) {Toast.maketext (Querynumberactivity.this, "number cannot be empty", 0 ). Show ();} else{//asynchronously queries the database to obtain the attribution information displayed to Txetview on the new Querynumbertask (). Execute (text);}});} public class Querynumbertask extends Asynctask<string, Void, string>{@Overrideprotected String doinbackground ( String ... params) {string number = params[0];//queries the database for the attribution information return addressservice.getaddress (number);} @Overrideprotected void OnPostExecute (String result) {Tv_show.settext (result);}}}
Effect: