您的位置:首页 > 移动开发 > Android开发

android134 360 07 归属地查询

2016-01-10 14:49 330 查看


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
style="@style/TitleStyle"
android:text="归属地查询" />

<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="query"
android:text="查询" />

<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="查询结果"
android:textSize="18sp" />

</LinearLayout>


package com.itheima52.mobilesafe.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.itheima52.mobilesafe.R;
import com.itheima52.mobilesafe.db.dao.AddressDao;

/**
* 归属地查询页面
* 1.网络查询
* 2.数据库查询
*/
public class AddressActivity extends Activity {

private EditText etNumber;
private TextView tvResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_address);
etNumber = (EditText) findViewById(R.id.et_number);
tvResult = (TextView) findViewById(R.id.tv_result);

// 监听EditText的变化
etNumber.addTextChangedListener(new TextWatcher() {
// 文字 发生变化时的回调
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {
String address = AddressDao.getAddress(s.toString());
tvResult.setText(address);
}
// 文字变化前的回调
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
// 文字变化结束之后的回调
@Override
public void afterTextChanged(Editable s) {
}
});
}

/**
* 开始查询
*/
public void query(View view) {
String number = etNumber.getText().toString().trim();
if (!TextUtils.isEmpty(number)) {
String address = AddressDao.getAddress(number);
tvResult.setText(address);
} else {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);//动画
/*res/anim/shake.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"            X移动坐标0到10,不用差补器则只是从0移动到10然后就不动了,
android:interpolator="@anim/cycle_7"  差补器
android:toXDelta="10" />
<!--
res/anim/cycle_7.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />  一秒钟内移动7次,
-->
*/

/* shake.setInterpolator(new Interpolator() {//自定义差补器
@Override
public float getInterpolation(float x) {
//y=ax+b
return y;//y=x表示是匀速运动
}
});*/
etNumber.startAnimation(shake);//输入框左右摆动
vibrate();
}
}

/**
* 手机震动, 需要权限 android.permission.VIBRATE
*/
private void vibrate() {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
// vibrator.vibrate(2000);震动两秒
vibrator.vibrate(new long[] { 1000, 2000, 1000, 3000 }, -1);// 先等待1秒,再震动2秒,再等待1秒,再震动3秒,
// 参2等于-1表示只执行一次
// 参2等于0表示从数组第0个文字开始循环,
// 参2表示从数组第几个位置开始循环,数组可以无限的写下去。
// 取消震动vibrator.cancel()
}
}


package com.itheima52.mobilesafe.db.dao;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
* 拷贝数据库(从assets目录拷贝到data/data/com.itheima52.mobilesafe/files/address.db)
*/
private void copyDB(String dbName) {//"address.db"
// File filesDir = getFilesDir();
// System.out.println("路径:" + filesDir.getAbsolutePath());  data/data/com.itheima52.mobilesafe/files
File destFile = new File(getFilesDir(), "address.db");// 要拷贝的目标地址,前面是文件夹后面是文件名,也可以new File(getFilesDir() + "/address.db"),
if (destFile.exists()) {//不要重复拷贝
System.out.println("数据库" + "address.db" + "已存在!");
return;
}
FileOutputStream out = null;
InputStream in = null;
try {
in = getAssets().open("address.db");//获取assets目录下的address.db文件。
out = new FileOutputStream(destFile);
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* 归属地查询工具
*/
public class AddressDao {

private static final String PATH = "data/data/com.itheima52.mobilesafe/files/address.db";// 注意,该路径必须是data/data目录的文件,不能是assets的目录,否则数据库访问不到

public static String getAddress(String number) {
String address = "未知号码";//没有查出来
// 获取数据库对象
SQLiteDatabase database = SQLiteDatabase.openDatabase(PATH, null,SQLiteDatabase.OPEN_READONLY);//因为数据库是现成的所以不用SQLHelper来创建数据库。第三个是访问模式。
// 手机号码特点: 1 + (3,4,5,6,7,8) + (9位数字),^1[3-8]\d{9}$
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.moveToNext()) {//只有一条数据所以只用if不用while
address = cursor.getString(0);
}
cursor.close();
} else if (number.matches("^\\d+$")) {// 匹配数字
switch (number.length()) {
case 3:
address = "报警电话";
break;
case 4:
address = "模拟器";
break;
case 5:
address = "客服电话";
break;
case 7:
case 8:
address = "本地电话";
break;
default:
// 01088881234
// 048388888888
if (number.startsWith("0") && number.length() > 10) {// 有可能是长途电话
// 有些区号是4位,有些区号是3位(包括0)
// 先查询4位区号
Cursor cursor = database.rawQuery(
"select location from data2 where area =?",
new String[] { number.substring(1, 4) });
if (cursor.moveToNext()) {//找到了
address = cursor.getString(0);
} else {//没有查到
cursor.close();
// 查询3位区号
cursor = database.rawQuery(
"select location from data2 where area =?",
new String[] { number.substring(1, 3) });
if (cursor.moveToNext()) {
address = cursor.getString(0);
}
cursor.close();
}
}
break;
}
}
database.close();// 关闭数据库
return address;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: