Android监听系统来电并弹出提示窗口-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Android监听系统来电并弹出提示窗口

1.问题

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、成都网站制作、成都外贸网站建设、白塔网络推广、微信小程序开发、白塔网络营销、白塔企业策划、白塔品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供白塔建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

项目中有自己企业的通讯录,但是在应用中拨打公司通讯录的联系人,由于手机通讯录中没有相应的信息,只显示一串电话号

2 .目的

监听系统来电,获取到电话号码,通过调用接口,查询出来相应电话号码的详细信息,并弹出系统悬浮框,给用户提示。

3.实现

首先 注册广播监听系统来电。监听系统来电需要、注册相应的权限

代码地址:https://github.com/sdsjk/phone_alert.git


自定义广播去监听系统来电

 public class PhoneReceiver extends BroadcastReceiver {

  private Context mcontext;


  @Override
  public void onReceive(Context context, Intent intent){
    mcontext=context;
    System.out.println("action"+intent.getAction());
    if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
      //如果是去电(拨出)
      Log.e("TAG","拨出");
    }else{

      Log.e("TAG","来电");
      TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
      tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
      //设置一个监听器
    }
  }


  private PhoneStateListener listener=new PhoneStateListener(){

    @Override
    public void onCallStateChanged(int state, final String incomingNumber) {
      // TODO Auto-generated method stub
      //state 当前状态 incomingNumber,貌似没有去电的API
      super.onCallStateChanged(state, incomingNumber);
      switch(state){
        case TelephonyManager.CALL_STATE_IDLE:
          Log.e("TAG","挂断");

          break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
          Log.e("TAG","接听");

          break;
        case TelephonyManager.CALL_STATE_RINGING:

          //输出来电号码
          Log.e("TAG","响铃:来电号码"+incomingNumber);
          Log.e("TAG","响铃:======"+Thread.currentThread().getName());

          break;
      }
    }
  };
  };

需要静态注册广播

 
      
        
        
        
      
    

其次在注册完,广播之后我们需要在监听到系统的来电之后,后获取到电话号之后去请求接口,获取数据。并弹出系统悬浮框。

注意:在弹出系统悬浮框的时候需要注册权限,并且检查应用的允许弹出悬浮框权限是否开启。

 

在监听中的 TelephonyManager.CALL_STATE_RINGING中操作

    inflate= LayoutInflater.from(mcontext);
              wm = (WindowManager)mcontext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
              WindowManager.LayoutParams params = new WindowManager.LayoutParams();
              params.type = WindowManager.LayoutParams.TYPE_PHONE;
              params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
              params.gravity= Gravity.CENTER;
              params.width = WindowManager.LayoutParams.MATCH_PARENT;
              params.height = 600;
              params.format = PixelFormat.RGBA_8888;
              phoneView=inflate.inflate(R.layout.phone_alert,null);
              wm.addView(phoneView, params);

自定义一个布局文件,作为要添加的View,布局文件如下

 <?xml version="1.0" encoding="utf-8"?>
  
  
  
  
  
  
    
    
  

  

使用到两个背景shape

 <?xml version="1.0" encoding="utf-8"?>
  
  
  
  

  <?xml version="1.0" encoding="utf-8"?>
  
  
  
  

广播中完整代码

 package com.cloud.adapter.myview;

  import android.app.Activity;
  import android.app.Service;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.graphics.PixelFormat;
  import android.os.Handler;
  import android.os.Looper;
  import android.telephony.PhoneStateListener;
  import android.telephony.TelephonyManager;
  import android.util.Log;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.WindowManager;
  import android.widget.TextView;

  /**
   * Created by zhang on 2017/10/10.
   */

  public class PhoneReceiver extends BroadcastReceiver {

  private Context mcontext;
  private WindowManager wm;

  @Override
  public void onReceive(Context context, Intent intent){
    mcontext=context;
    System.out.println("action"+intent.getAction());
    if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
      //如果是去电(拨出)
      Log.e("TAG","拨出");
    }else{
      //查了下android文档,貌似没有专门用于接收来电的action,所以,非去电即来电
      Log.e("TAG","来电");
      TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
      tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
      //设置一个监听器
    }
  }

  private TextView tv;
  private LayoutInflater inflate;
  private View phoneView;
  private PhoneStateListener listener=new PhoneStateListener(){

    @Override
    public void onCallStateChanged(int state, final String incomingNumber) {
      // TODO Auto-generated method stub
      //state 当前状态 incomingNumber,貌似没有去电的API
      super.onCallStateChanged(state, incomingNumber);
      switch(state){
        case TelephonyManager.CALL_STATE_IDLE:
          Log.e("TAG","挂断");
          wm.removeView(tv);
          break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
          Log.e("TAG","接听");
          wm.removeView(tv);
          break;
        case TelephonyManager.CALL_STATE_RINGING:

          inflate= LayoutInflater.from(mcontext);
          wm = (WindowManager)mcontext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
          WindowManager.LayoutParams params = new WindowManager.LayoutParams();
          params.type = WindowManager.LayoutParams.TYPE_PHONE;
          params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
          params.gravity= Gravity.CENTER;
          params.width = WindowManager.LayoutParams.MATCH_PARENT;
          params.height = 600;
          params.format = PixelFormat.RGBA_8888;
          phoneView=inflate.inflate(R.layout.phone_alert,null);
          wm.addView(phoneView, params);
          Log.e("TAG","响铃:来电号码"+incomingNumber);
          Log.e("TAG","响铃:======"+Thread.currentThread().getName());
          //输出来电号码
          break;
      }
    }
  };
  };

效果图

Android监听系统来电并弹出提示窗口

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


名称栏目:Android监听系统来电并弹出提示窗口
文章路径:http://kswsj.cn/article/pdceds.html

其他资讯