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); } }
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}$"))//手机号 { 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//固定电话 { int len = number.length(); switch (len) { case 4: city = "模拟器"; break; case 7: case 8: city = "本地号码"; break; case 10://3位区号+7位号码 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位区号+8位号码,4位区号+7位号码 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; } }
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; @Override protected void onCreate(Bundle savedInstanceState) { 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() { @Override public void onClick(View v) { String text = et_number.getText().toString(); if(text.trim().equals("")) { Toast.makeText(QueryNumberActivity.this,"号码不能为空",0).show(); }else { //异步查询数据库,获得归属地信息显示到txetview上 new QueryNumberTask().execute(text); } } }); } public class QueryNumberTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { String number = params[0]; //查询数据库,获取归属地信息 return AddressService.getAddress(number); } @Override protected void onPostExecute(String result) { tv_show.setText(result); } } }
【安卓笔记】单机版手机归属地查询,布布扣,bubuko.com
原文:http://blog.csdn.net/chdjj/article/details/38025025