96.s1-来电归属地查询

查询来电归属地查询的方法:通过使用数据库去查询,对所有的号段的数据进行保存,不同的号段有不同的归属地(address.db),数据库等和项目无关的文件,一般放在assets目录下面,使用数据库的时候必须在data/data/com.ldw.safe/files/这个目录下面使用,因此需要放在这个目录下面,才能使用数据库进行想关的查询操作

设置图片的padding:android:drawablePadding="5dp"。

设置内容的padding:android:padding="10dp"

添加点击事件:android:onClick="click"

设置可以点击:android:clickable="true"

初始化splash页面的时候,首先在这个页面拷贝一下数据库splashActivity.java,使用数据库的时候必须在data/data/com.ldw.safe/files/这个目录下面使用,因此需要放在这个目录下面

/*
	 * 拷贝数据库
	 */
	public void copyDB(String dbName){
		//获取数据库的文件路径,要拷贝的目标地址
		File targetFile = new File(getFilesDir(), dbName);//
		//数据库存在
		if(targetFile.exists()){
			System.out.println("数据库已经存在");
			return;
		}
		FileOutputStream out = null;
		InputStream in = null;
		try {
			in = getAssets().open(dbName);
			out = new FileOutputStream(targetFile);
			//读数据
			int len = 0;
			byte[] buffer = new byte[1024];
			
			while((len = in.read(buffer)) != -1 ){
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

高级设置-页面归属地查询页面activity_advancedtools.xml

<?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="高级工具"
        />
    <TextView 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:background="@drawable/button"
    	android:text="电话归属地查询"
    	android:textColor="@color/black"
    	android:drawableLeft="@android:drawable/ic_menu_camera"
    	android:gravity="center_vertical"
    	android:textSize="20sp"
    	android:drawablePadding="5dp"
    	android:padding="10dp"
    	android:clickable="true"
    	android:onClick="numberAddressQuery"
        />
    

</LinearLayout>

高级设置的业务逻辑AdvancedToolsActivity.java主要是跳转到号码归属地查询的页面

package com.ldw.safe.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.ldw.safe.R;

/*
 * 高级工具
 */
public class AdvancedToolsActivity extends Activity {

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advancedtools);
	}
	
	/*
	 * 归属地查询
	 */
	public void numberAddressQuery(View v){
		startActivity(new Intent(this, AddressActivity.class));
	}
}

数据号码股i属地查询的主页activity_address.xml

<?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="phone"
	    />
	<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:text="查询结果"
	    android:textSize="20sp"
	    android:layout_marginLeft="5dp"
	    />
</LinearLayout>

查询归属地的业务逻辑AddressActivity.java调用数据库逻辑文件查询

package com.ldw.safe.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ldw.safe.R;
import com.ldw.safe.db.dao.AddressDao;

/*
 * 归属地穿页面
 */
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);
	}
	
	/*
	 * 开始查询
	 */
	public void query(View v){
		String number = etNumber.getText().toString().trim();
		if(!TextUtils.isEmpty(number)){
			String address = AddressDao.getAddress(number);
			tvResult.setText(address);
		}
	}
		
		
}

数据库查询的逻辑文件AddressDao.java使用正则表达式判断用户的输入的号码

 

package com.ldw.safe.db.dao;

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

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

	//数据库的路径,前面是data/data后面跟着包的名字,路径必须要这样写
	private static final String path ="data/data/com.ldw.safe/files/address.db";
	public static String getAddress(String number){
		
		String address = "未知号码";
		
		//读取数据库
		SQLiteDatabase database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
		//对查询的号码添加限制,只能查询手机号码,不能随意输入
		//查询手机号码只需要匹配前7位,后面的不用查询
		// 手机号码特点: 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()) {
				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;
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值