手机归属地查询
效果图:
分析:
1、传递多个参数,用一个类就好
2、打开数据库
private SQLiteDatabase database;
database=SQLiteDatabase.openOrCreateDatabase(file, null);
file是数据库的路径
3、在逻辑中多加判断
比如是否获取到正确的手机号
比如我们操作的字符串是否为空
比如时候获取正确参数
4、通过文件流来实现释放APK中包中的数据库文件到手机本地
5、需要用的数据库放在assets目录中
bufferIn = new BufferedInputStream(getAssets().open("naddress.db"));
6、确保输出流flush,如果不flush,数据会变少,有一部分没有成功写出到本地
bufferOut.flush();
7、try-catch的时候记得finally,去关闭输入流和输出流
8、-1为文件末
while ((len = bufferIn.read(buffer)) != -1)
9、File file = getDatabasePath("naddress.db");
这个方法得到这样的路径data/data/包/database/naddress.db
10、如果文件没有成功创建,我们取创建文件夹
if (!file.exists())
file.getParentFile().mkdirs();
11、正则表达式匹配手机号
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Matcher m = p.matcher(phoneNumber);
if (!m.matches())
12、百度的正确姿势
java 正则表达式验证手机号
13、找错误的时候多去看cause by
代码:
/查询手机号归属地2/res/layout/activity01.xml
1 26 7 11 12 22 2316 17 21 30 31 38 39
database.AddressDao
1 package database; 2 3 import java.io.File; 4 5 import bean.InfoBean; 6 import android.database.Cursor; 7 import android.database.sqlite.SQLiteDatabase; 8 9 /*10 * 操作数据库11 */12 public class AddressDao {13 private SQLiteDatabase database;14 public AddressDao(File file){15 database=SQLiteDatabase.openOrCreateDatabase(file, null);16 }17 /**18 * 获取城市及卡类型19 * @param mobilePrefix 手机号码前缀20 */21 public InfoBean getCityOrCardType(String mobilePrefix){22 String sql="select city,cardtype from address_tb where _id in(select outkey from numinfo where mobileprefix=?);";23 Cursor cursor=database.rawQuery(sql, new String[]{mobilePrefix});24 //就一条记录,不用用while,就if就好了25 if(cursor.moveToNext()){26 String city=cursor.getString(cursor.getColumnIndex("city"));27 String cardtype=cursor.getString(cursor.getColumnIndex("cardtype"));28 return new InfoBean(city, cardtype);29 }30 return null;31 }32 }
bean.InfoBean
1 package bean; 2 3 public class InfoBean { 4 private String city; 5 private String cardType; 6 7 8 public InfoBean(String city, String cardType) { 9 super();10 this.city = city;11 this.cardType = cardType;12 }13 public String getCity() {14 return city;15 }16 public void setCity(String city) {17 this.city = city;18 }19 public String getCardType() {20 return cardType;21 }22 public void setCardType(String cardType) {23 this.cardType = cardType;24 }25 26 27 }
fry.Activity01
1 package fry; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.util.regex.Matcher; 9 import java.util.regex.Pattern; 10 11 import bean.InfoBean; 12 13 import com.example.searchMobileCity.R; 14 15 import database.AddressDao; 16 import android.app.Activity; 17 import android.os.Bundle; 18 import android.view.View; 19 import android.view.View.OnClickListener; 20 import android.widget.Button; 21 import android.widget.EditText; 22 import android.widget.TextView; 23 import android.widget.Toast; 24 25 public class Activity01 extends Activity { 26 private Button btn_search; 27 private EditText et_mobileNum; 28 private TextView tv_city_cardType; 29 private AddressDao dao; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 // TODO Auto-generated method stub 34 super.onCreate(savedInstanceState); 35 setContentView(R.layout.activity01); 36 initView(); 37 // file就是数据库文件路径 38 File file = initDatabaseData(); 39 dao = new AddressDao(file); 40 41 } 42 43 private void initView() { 44 tv_city_cardType = (TextView) findViewById(R.id.tv_city_cardType); 45 et_mobileNum = (EditText) findViewById(R.id.et_mobileNum); 46 btn_search = (Button) findViewById(R.id.btn_search); 47 } 48 49 /* 50 * 查询归属地 51 */ 52 public void query(View view) { 53 tv_city_cardType.setText("归属地:"); 54 String phoneNumber = et_mobileNum.getText().toString(); 55 Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); 56 Matcher m = p.matcher(phoneNumber); 57 if (!m.matches()) { 58 Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); 59 return ; 60 } else { 61 String mobilePrefix = phoneNumber.substring(0, 7); 62 InfoBean bean = dao.getCityOrCardType(mobilePrefix); 63 if (bean == null) { 64 tv_city_cardType.setText("没查询到该号码的城市与归属地!!"); 65 } else { 66 tv_city_cardType.setText("城市:" + bean.getCity() + " \n卡类型: " 67 + bean.getCardType()); 68 } 69 } 70 } 71 72 /* 73 * 释放APK中包中的数据库文件到手机本地 读和写 74 */ 75 private File initDatabaseData() { 76 // 这个方法得到这样的路径data/data/包/database/naddress.db 77 File file = getDatabasePath("naddress.db"); 78 if (!file.exists()) { 79 file.getParentFile().mkdirs(); 80 } else { 81 return file; 82 } 83 84 // 获取读入流 85 BufferedInputStream bufferIn = null; 86 // 输出流 87 BufferedOutputStream bufferOut = null; 88 try { 89 bufferIn = new BufferedInputStream(getAssets().open("naddress.db")); 90 bufferOut = new BufferedOutputStream(new FileOutputStream(file)); 91 // 开始释放缓存流 92 // 读的时候,弄个缓存区,让释放快一点 93 byte[] buffer = new byte[8000]; 94 int len = 0; 95 // -1为文件末 96 while ((len = bufferIn.read(buffer)) != -1) { 97 bufferOut.write(buffer, 0, len); 98 // 不flush的话文件大小会变小的 99 bufferOut.flush();100 }101 return file;102 } catch (IOException e) {103 // TODO Auto-generated catch block104 e.printStackTrace();105 } finally {106 try {107 if (bufferIn != null)108 bufferIn.close();109 if (bufferOut != null)110 bufferOut.close();111 } catch (IOException e) {112 e.printStackTrace();113 }114 }115 return null;116 }117 }