Android中实现手机号码归属地查询的示例代码

分类:手机开发| 发布:佚名| 查看: | 发表时间:2014/10/29

目前手机里面的号码归属地查询主要是通过两种方式:1.联网查询,2.匹配本机归属地数据库。

我认为两种结合方式最好,在本地数据库中匹配不到的在进行联网查询,能大大增加匹配效果,并且不用过于增加本地数据库容量而增大安装包大小。

步骤:1.开启软件的时候把数据库从assets目录拷贝到files目录下,如果已存在,则不用重新拷贝。

2.实现界面。

3.实现工具类PhoneAddressUtils的getPhoneAddress()方法

4.在界面类activity里调用工具类方法得到地址兵显示出来。

 

首先是复制数据库的操作:

private void copyDB() {
    File file = new File(getFilesDir(), "address.db");
    if(file.exists()&&file.length()>0) {
        Toast.makeText(this, "已经复制数据库", 0).show();
    } else {
        try {
            AssetManager am = getAssets();
             
            byte[] buffer = new byte[1024];
            InputStream is = am.open("address.db");
            FileOutputStream fis = new FileOutputStream(file);
            int len = 0;
            while((len=is.read(buffer))>0) {
                fis.write(buffer, 0, len);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


因为运行中工具类得不到assets目录下的文件,所以要在在开机启动的SplashActivity中把数据库拷贝到files目录下。

获得assert目录下的文件要用AssetManager对象的open()方法,打开文件返回输入流。

实现界面:就一个简单的输入框,按钮,显示框就行。

工具类:

package com.itheima.mobilesafe.db.dao;
 
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
 
public class AddressDao {
    private static String path = "data/data/com.itheima.mobilesafe/files/address.db";
    public static String getAddress(String number) {
         
        String address = number;
        if(number.matches("^1[34568]\d{9}$")) {
            String sql = "select location from data2 where id = (select outkey from data1 where id= ?)";   
            SQLiteDatabase database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
            Cursor cursor = database.rawQuery(sql, new String[] {number.substring(0, 7)});
            while(cursor.moveToNext()) {
                address = cursor.getString(0);
            }
            cursor.close();
            database.close();
        } else {
            address = "不是手机号码";
        }
         
        return address;
    }
}

调用显示:

点击按钮时:

public void queryAddress(View view) {
        String number = et_phone.getText().toString();
        String address = AddressDao.getAddress(number);
        et_address.setText(address);
    }

但是我们要动态的显示位置,所以要对输入框加一个TextChangedListener,当输入字符串大于3的时候,自动调用进行匹配显示出来。

et_phone.addTextChangedListener(new TextWatcher() {
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
          // TODO Auto-generated method stub
          if(s.length()>3) {
              String address = AddressDao.getAddress(s.toString());
              et_address.setText(address);
          }
      }
       
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count,
              int after) {
          // TODO Auto-generated method stub
           
      }
       
      @Override
      public void afterTextChanged(Editable s) {
          // TODO Auto-generated method stub
           
      }
  });
365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/mobile-dev/1703.html