【安卓笔记】单机版手机归属地查询

既然是单机版,那么必然是查询本地数据库了,所以我们得准备一个离线数据库文件(下载地址:http://download.csdn.net/detail/rowandjj/7660979).
步骤:
1.创建一个工具类打开数据库:
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.编写业务方法:
首先需要使用正则表达式判断号码是手机号码还是固定电话。若是手机号码则根据前面7位查询数据库。
若是固定电话,则先要判断固定电话的长度,分下面几种情况:
3位区号+7位号码
3位区号+8位号码
4位区号+7位号码
4位区号+8位号码
根据区号即可查出归属地
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;
	}
}

3.调用业务类,查询手机归属地信息:
由于是数据库操作,所以使用了AsyncTask进行异步查询。
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

【安卓笔记】单机版手机归属地查询

上一篇:如何从ios酷我音乐盒中导出已下载的音乐文件(使用Java编程实现)


下一篇:学习笔记108— Win10 修改用户名,注销之后又还原了