17、高级工具--号码归属地查询

号码归属地数据库文件直接使用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>

NumberQueryActivity的实现代码:

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);
                }
            }
        });
    }
}

运行效果图:





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

asmcvc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值