您的位置:首页 > 其它

【安卓注意事项】单机版的手机归属地查询

2015-09-22 19:48 513 查看
既然是单机版,那么必定是查询本地数据库了。所以我们得准备一个离线数据库文件(下载地址: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);
}
}
}


效果:




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: