android中电话归属地查询

来源:互联网 发布:淘宝如何退换货物 编辑:程序博客网 时间:2024/05/01 19:35
来电归宿地查询有两种查询方式:
1、网络平台查询;
2、数据库查询;

这是使用数据库查询的方式,实现步骤如下:
1、 在Eclispase的android工程中把要发布的数据库放在assets目录下面;
2、在展示应用的引导页面(也就是展示广告或者公司logo的页面)把数据库拷贝到”data/data/应用名称/files/数据库名称”路径下,在实现数据库拷贝的代码中有判断数据库是否存在的逻辑(当用户第一次安装应用时数据库就拷贝了,当用户又一次打开应用时无需再拷贝,这样可以节省应用的性能)。拷贝数据库的工具类如下:
<textarea readonly="readonly" name="code" class="java"> package gif.phone.zzy.com.comphonesafe.com.mobliesafe.activity.databaseutils;import android.content.Context;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import gif.phone.zzy.com.comphonesafe.R;import gif.phone.zzy.com.comphonesafe.com.mobliesafe.activity.util.ToastUtils;/** * 拷贝assets文件夹下的数据库到  data/data/应用包名/files文件夹 */public class DatabaseUtils {    public static void copyDataBase(Context context,String dataBaseName){        File desFile=new File(context.getFilesDir(),dataBaseName);        //判断数据库是否存在,如果存在就不在往desFile中拷贝数据库        if (desFile.exists()){            return;        }        FileOutputStream fos=null;        InputStream inputStream=null;       //context.getResources().openRawResource(R.raw.address);        try {             inputStream = context.getAssets().open(dataBaseName);             fos=new FileOutputStream(desFile);            byte[] bytes=new byte[1024];            int lenth=0;            while ((lenth=inputStream.read(bytes))!=-1){                fos.write(bytes,0,bytes.length);                fos.flush();            }        } catch (IOException e) {            ToastUtils.showToast(context,"assets目录下的数据库不存在");        }finally {            if (inputStream!=null){                try {                    inputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (fos!=null){                try {                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}</textarea>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.71429; font-size: 14px;"><span style="font-family: 'Helvetica Neue'; color: rgb(51, 51, 51);"> 3、实现归属地查询的工具类 </span></div> <pre name="code" class="java">  <textarea readonly="readonly" name="code" class="java> package gif.phone.zzy.com.comphonesafe.com.mobliesafe.activity.databaseutils;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Environment;import java.io.File;import gif.phone.zzy.com.comphonesafe.com.mobliesafe.activity.util.LogUtils;/** * 查询归属地工具类 */public class AddressQuery  {    private static  String dataBaseNameFilepath;    public static String getAdress(Context context,String number){        String address="未知号码";        dataBaseNameFilepath= context.getFilesDir()+ File.separator+"address.db";        //获取数据库        SQLiteDatabase database=SQLiteDatabase.openDatabase(dataBaseNameFilepath,null,SQLiteDatabase.OPEN_READONLY);        //开始查询        //正则表达式匹配电话号码        if (number.matches("^1[3-8]\\d{9}$")){//匹配电话号码            Cursor cursor = database.rawQuery("select location from data2 where id  =(select outkey from data1 where id=?)",                    new String[]{number.substring(0, 7)});//查电话号码只需要前七位即可            if (cursor!=null){                if (cursor.moveToNext()){                    address=cursor.getString(0);                }            }            cursor.close();        }else if (number.matches("^\\d+$")){//匹配数字            switch (number.length()){                case 3:                    if (number.equals("110")){                        address="报警电话";                    }else if (number.equals("120")){                        address="急救电话";                    }                    break;                case 4:                    address="android模拟器电话";                    break;                case 5:                    address="客服电话";                    break;                case 7:                case 8:                    address="本地电话";                    break;                default:                if (number.startsWith("0") && number.length()>10){//判断长途或者本地座机                    LogUtils.showLog("判断长途或者本地座机");                    Cursor cursor = database.rawQuery("select location from data2 where area=?",                            new String[]{number.substring(1, 4)});//截取区号的后三位   如贵阳区号0851                    if (cursor.moveToNext()){                        address = cursor.getString(0);                    }else {                        cursor.close();                        cursor = database.rawQuery("select location from data2 where area=?",                                new String[]{number.substring(1, 3)});//截取区号的后两位,如北京区号010                        if (cursor.moveToNext()){                            address=cursor.getString(0);                            cursor.close();                        }                    }                }                    break;            }        }        database.close();        return address;    }}</textarea>



0 0
原创粉丝点击