Android显示来去电归属地

前言

最近老是被什么基金原油的电话骚扰,但是手机没显示归属地,搞得我有时以为是快递电话之类的就接了..然后各种烦..所以打算做一个来去电显示归属地的小软件,碰到某些城市的陌生号码就直接挂掉,既然要做就顺便写篇博客把,显示来去电归属地这个功能商业app用得不多,但是权当学习了。

实现原理

网上找一个有号码段的归属地数据库,然后开启个服务监听系统去电广播和来电,然后获取来去电号码,跟数据库的号码字段进行比对,然后显示在手机界面上。

实现步骤

下面一步步来完成这个功能:
主界面布局:
<?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">

    <Button
        android:id="@+id/btn_show_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示来去电归属地"
        android:textSize="19sp" />

    <Button
        android:id="@+id/btn_noshow_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="不显示来去电归属地"
        android:textSize="19sp" />
</LinearLayout>
这个布局是自己为了自己设置是否显示归属地的。
在main下面新建一个assets文件夹,并把网上找到的数据库拷贝到该文件夹下面
接下来分析数据库表:
我下的数据库里面有两个表,表结构如下:
data1:

data2:

其中data1的id就是电话号码,前七位就可以确定归属地了,然后根据id对应的outkey去匹配data2的id就可以得到归属地信息了。知道了两个表之间的联系之后就可以开始我们的查询工作了。
新建一个CallAddressDao类,这个类是用来访问数据库的
public class CallAddressDao {
    //数据库路径
    private static final String PATH = "data/data/com.sjr.calladdress/files/address.db";
    /**
     * 从数据库中获取手机归属地
     * @param number
     * @return 手机归属地
     */
    public static String getCallAddress(String number){
        String callAddress = "未知号码";
        //获取数据库对象
        SQLiteDatabase database = SQLiteDatabase.openDatabase(PATH, null,
                SQLiteDatabase.OPEN_READONLY);
        //正则表达式匹配
        if (number.matches("^1[3-8]\\d{9}$")) {//匹配器11位手机号
            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())
                callAddress = cursor.getString(0);
            cursor.close();
        }else if (number.matches("^\\d+$")){//匹配数字
            switch (number.length()){
                case 3:
                    callAddress = "报警电话";//三位数就是报警电话
                    break;
                case 4:
                    callAddress = "模拟器";
                    break;
                case 5:
                    callAddress = "客服电话";
                    break;
                case 7:
                case 8:
                    callAddress = "本地电话";
                    break;
               
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提供的引用内容,Android Studio本身并不能实现来电位置实时显示的功能。不过,你可以通过在应用中集成相关的API来实现该功能。具体来说,你可以使用Android TelephonyManager API来获取来电信息,使用Android Location API来获取设备位置信息,然后将这些信息结合起来在应用中实现来电位置实时显示的功能。 以下是一个简单的示例代码,演示如何使用TelephonyManager API获取来电信息和使用Location API获取设备位置信息: ```java import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView mLocationTextView; private TelephonyManager mTelephonyManager; private PhoneStateListener mPhoneStateListener; private LocationManager mLocationManager; private LocationListener mLocationListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocationTextView = (TextView) findViewById(R.id.location_text_view); // 获取TelephonyManager实例 mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // 创建PhoneStateListener实例 mPhoneStateListener = new PhoneStateListener() { @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); // 当来电状态为响铃或接听时,获取来电位置信息并显示在TextView中 if (state == TelephonyManager.CALL_STATE_RINGING || state == TelephonyManager.CALL_STATE_OFFHOOK) { String location = getLocation(); mLocationTextView.setText(location); } } }; // 注册PhoneStateListener mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); // 获取LocationManager实例 mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 创建LocationListener实例 mLocationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { // 当设备位置发生变化时,更新TextView中的位置信息 String locationStr = "Latitude: " + location.getLatitude() + "\nLongitude: " + location.getLongitude(); mLocationTextView.setText(locationStr); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; // 检查是否已经获取了定位权限 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 如果没有定位权限,则请求权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0); } else { // 如果已经获取了定位权限,则注册LocationListener mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener); } } @Override protected void onDestroy() { super.onDestroy(); // 取消注册PhoneStateListener和LocationListener mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE); mLocationManager.removeUpdates(mLocationListener); } // 获取来电位置信息的方法 private String getLocation() { String locationStr = "Unknown"; // 检查是否已经获取了定位权限 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 如果没有定位权限,则返回Unknown return locationStr; } // 获取最近一次的设备位置信息 Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { // 如果获取到了设备位置信息,则返回位置信息字符串 locationStr = "Latitude: " + location.getLatitude() + "\nLongitude: " + location.getLongitude(); } return locationStr; } } ``` 请注意,上述代码仅仅是一个示例,实际上你需要根据具体的需求和场景来编写代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值