[mobilesafe] 05_手机固话号码归属地离线查询

Android 4.0

手机号码归属地离线查询

select location from data2 where id = (select outkey from data1 where id = '1384444');
1、NumberQueryActivity.java
package cn.zengfansheng.mobilesafe;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import cn.zengfansheng.mobilesafe.dao.AddressDao;
import cn.zengfansheng.mobilesafe.utils.ToastUtils;
/**
 * 12、手机号码归属地查询Activity界面
 * @author hacket
 */
public class NumberQueryActivity extends Activity {
    private EditText et_phone_number;
    private EditText et_query_address;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_number_query);
        et_phone_number = (EditText) this.findViewById(R.id.et_phone_number);
        et_query_address = (EditText) this.findViewById(R.id.et_query_address);
    }
    
    /**
     * 1、查询号码归属地
     * @param view
     */
    public void queryAddressByPhoneNum(View view){
        String phoneNum = et_phone_number.getText().toString().trim();// 待手机号码
        if (TextUtils.isEmpty(phoneNum)) {// 1、输入为null
            ToastUtils.showToastInThread(this"号码不能为空!");
            et_query_address.setText("");
            return;
        }
        try {
            String address = AddressDao.getAddress(this, phoneNum);
            et_query_address.setText(address);
        } catch (IOException e) {
            e.printStackTrace();
            ToastUtils.showToastInThread(this"查询手机号码地址失败!");
        }
    }
}
2、 AddressDao.java
package cn.zengfansheng.mobilesafe.dao;
 
import java.io.IOException;
 
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
 
/**
* 1、访问手机号码的DAO:Data Access Object
* @author hacket
*/

public class AddressDao {
 
    /**
     * 1、根据手机号码查询归属地
     *
     * @param context上下文
     * @param phoneNum 待查询的号码
     * @return 查询到的地址,没有则返回查询的号码,输入不对,返回null。
     * @throws IOException
     */

    public static String getAddress(Context context,String phoneNum) throws IOException {
 
        String path = context.getFilesDir() + "/phoneAddress.db";
        SQLiteDatabase database = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READONLY);
 
        String location = phoneNum;//电话号码归属地
 
        // 判断手机号码类型 手机/ 固定电话
        // 手机1开头 3 4 5 8 /9位的数字
        if (phoneNum.matches("^1[3458]\\d{9}$")) {// 1、输入的是手机格式:11位数
            // 3、截取手机号码前7位
            phoneNum = phoneNum.substring(0, 7);
            String sql = "select location from data2 where id = (select outkey from data1 where id = ?)";
            Cursor cursor = database.rawQuery(sql, new String[] { phoneNum });
            if (cursor.moveToNext()) {
                location = cursor.getString(cursor.getColumnIndex("location"));
            }
            cursor.close();
            database.close();
        } else { // 2、不是手机格式,那么就是固话格式
            // 固定电话.
            // 110 119 120
            switch (phoneNum.length()) {
            case 3:
                location = "报警电话";
                break;
 
            case 4:
                location = "模拟器";
                break;
 
            case 5:
                location = "特殊客服";
                break;
 
            case 7:
                location = "本地电话";
 
                break;
            case 8:
                location = "本地电话";
                break;
 
            default:// 其他情况,前缀可能是3位数,也可能是4位数
 
                if (phoneNum.startsWith("0") && phoneNum.length() >= 10) {// 固话都是以0开始的
 
                    String prefix3 = phoneNum.substring(1, 3);// 3位数的前缀,去掉0
                    String prefix4 = phoneNum.substring(1, 4);// 4位数的前缀,去掉第一个0
 
                    //1、先查询是否是3位的区号
                    Cursor cursor = database.rawQuery("select location from data2 where area = ?",new String[]{prefix3} );;
                    if (cursor.moveToNext()) {
                        String mylocation = cursor.getString(cursor.getColumnIndex("location"));
                        location = mylocation.substring(0, mylocation.length()-2);
                    }
                    cursor.close();
                    cursor = database.rawQuery("select location from data2 where area = ?",new String[]{prefix4} );
                    if (cursor.moveToNext()) {
                        String mylocation = cursor.getString(cursor.getColumnIndex("location"));
                        location = mylocation.substring(0, mylocation.length()-2);
                    }
                    cursor.close();
                }
                break;
            }
            database.close();
        }
        return location;
    }       
}