号码归属地数据库文件直接使用assets/naddress.db,号码归属地查询类NumberAddressDao:
package com.example.mobilesafe.db; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.io.File; /** * Created by sing on 14-1-10. * desc:号码归属地查询类 */ public class NumberAddressDao { public static final String SQL_SELECT_MOBILEPREFIX = "select city from address_tb where _id=(select outkey from numinfo where mobileprefix=?)"; public static final String SQL_SELECT_AREA = "select city from address_tb where _id=(select outkey from numinfo where mobileprefix=?)"; private Context context; public NumberAddressDao(Context context) { this.context = context; } public String getAddress(String number) { String address = number; File file = new File(context.getFilesDir(), "address.db"); String path = file.getAbsolutePath(); SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); if (db.isOpen()) { if (number.matches("^1[3458]\\d{9}$")) { //匹配手机的前7位 Cursor cursor = db.rawQuery(SQL_SELECT_MOBILEPREFIX, new String[]{number.substring(0, 7)}); if (cursor.moveToFirst()) { address = cursor.getString(0); } cursor.close(); }else{ Cursor cursor; switch (number.length()) { case 4: address = "模拟器"; break; case 7: case 8: //一般是不带区号的本地号码 address = "本地号码"; break; case 10: //带有三位区号的号码 cursor = db.rawQuery(SQL_SELECT_AREA, new String[]{number.substring(0, 3)}); if (cursor.moveToFirst()) { address = cursor.getString(0); } cursor.close(); break; case 12: //带有四位区号的号码 cursor = db.rawQuery(SQL_SELECT_AREA, new String[]{number.substring(0, 4)}); if (cursor.moveToFirst()) { address = cursor.getString(0); } cursor.close(); break; case 11: //三位区号号码+8位号码或者是四位区号+7位号码 cursor = db.rawQuery(SQL_SELECT_AREA, new String[]{number.substring(0, 3)}); if (cursor.moveToFirst()) { address = cursor.getString(0); } cursor.close(); cursor = db.rawQuery(SQL_SELECT_AREA, new String[]{number.substring(0, 4)}); if (cursor.moveToFirst()) { address = cursor.getString(0); } cursor.close(); break; } } } return address; } }
创建android component:NumberQueryActivity,并勾选layout文件的创建选项:numberqueryactivity_layout.xml。
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView style="@style/title_center_text" android:text="号码归属地查询"/> <View style="@style/splitter_view"/> <EditText android:id="@+id/et_number" android:layout_height="wrap_content" android:layout_width="match_parent" android:hint="请输入查询的电话号码"/> <Button android:id="@+id/bt_query" android:text="查询" android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="match_parent" /> <TextView style="@style/content_text" android:id="@+id/tv_area" android:text="归属地:"/> </LinearLayout>
package com.example.mobilesafe; import android.app.Activity; import android.os.Bundle; import android.view.View; 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 com.example.mobilesafe.db.NumberAddressDao; /** * Created by sing on 14-1-10. * desc: */ public class NumberQueryActivity extends Activity { private EditText et_number; private Button bt_query; private TextView tv_area; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.numberqueryactivity_layout); et_number = (EditText) findViewById(R.id.et_number); bt_query = (Button) findViewById(R.id.bt_query); tv_area = (TextView) findViewById(R.id.tv_area); //点击查询 bt_query.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String number = et_number.getText().toString().trim(); if (number.isEmpty()) { Toast.makeText(NumberQueryActivity.this, "号码不能为空", 1).show(); //震动输入框 Animation shake = AnimationUtils.loadAnimation(NumberQueryActivity.this, R.anim.shake); et_number.startAnimation(shake); }else { NumberAddressDao numberAddressDao = new NumberAddressDao(NumberQueryActivity.this); String address = numberAddressDao.getAddress(number); tv_area.setText(address); } } }); } }
运行效果图: