来电归属地显示

1、创建一个监听来电的服务,在服务中查找到电话的归宿地,再在服务中Tost 归属地,代码如下:


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;


/**
 * 监听电话来电状态
 */
public class AddressService extends Service {
    private TelephonyManager tm;
    private MyPhoneStateListener myPhoneStateListener;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        tm= ((TelephonyManager) getSystemService(TELEPHONY_SERVICE));
        myPhoneStateListener = new MyPhoneStateListener();
        tm.listen(myPhoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
    }

    class MyPhoneStateListener extends PhoneStateListener{
        @Override//监听来电状态
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state){
                case TelephonyManager.CALL_STATE_RINGING://电话响铃
                    LogUtils.showLog("监听来电状态:CALL_STATE_RINGING");
                    //显示来电归属地
                    String adress = AddressQuery.getAdress(AddressService.this, incomingNumber);
                    ToastUtils.showToast(AddressService.this,adress);
                    break;

            }
            super.onCallStateChanged(state, incomingNumber);

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        //注销不能控制的后台的监听服务 PhoneStateListener.LISTEN_NONE参数就是代表取消监听服务
        tm.listen(myPhoneStateListener,PhoneStateListener.LISTEN_NONE);
    }
}

2、在与用户的交互过程中,当需要显示来电归属地的时候就启动服务startService(),当不需要显示归属地的时候就停止服务StopService(),代码如下:

  //用户在SettingActivity来电归属地的设置状态应
        sv_phoneSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sv_phoneSetting.ischeckBooks()){
                    //当归属地开关关闭时停止电话来电的监听服务
                    sv_phoneSetting.setCheckBooks(false);
                    stopService(new Intent(SittingActivity.this,AddressService.class));
                }else {
                    //当归属地开关打开时绑定电话来电的监听服务
                    sv_phoneSetting.setCheckBooks(true);
                    startService(new Intent(SittingActivity.this, AddressService.class));
                }
            }
        });

3、当用户手机上装有软件管家时有可能会停止用户启动的显示来电归属地的服务,这时为了保证用户开启的归属地显示状态和实际的来电归属地的服务补调一致,就引入                   ActivityManager系统服务;判断归属地服务是否存在。首先判断服务是否的工具类如下:

/**
 * 检测服务是否正在运行工具类
 */
public class ServiceStateUtils {
    private static ActivityManager am;
    public static boolean isRunningService(Context context, String serviceName){
        am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningServiceInfo> runningServices = am.getRunningServices(100);
        for (ActivityManager.RunningServiceInfo runningService : runningServices) {
            String className = runningService.service.getClassName();//得到服务的名称
            LogUtils.showLog("开启的服务:"+className);
            if (className.equals(serviceName)){
                return true;
            }
        }
        return false;
    }


}
   

    拿到归属地服务的状态设置UI,代码如下:

        sv_phoneSetting = ((SettingView) findViewById(R.id.sv_addressSetting));

        //当手动停止后台的来电服务时,用户在SettingActivity来电归属地的设置状态应和来电服务时的开闭状态一致
        boolean runningService = ServiceStateUtils.isRunningService(this, "gif.phone.zzy.com.comphonesafe.com.mobliesafe.activity.service.AddressService");
        if (runningService){
            sv_phoneSetting.setCheckBooks(true);
        }else {
            sv_phoneSetting.setCheckBooks(false);
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值