欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中监听未接来电的2种方法

程序员文章站 2022-06-16 10:53:11
这里主要是总结一下如何监听有未接来电的问题   1.1 使用广播接收器 brocastreceiver 实现思路 : 静态注册监听android.int...

这里主要是总结一下如何监听有未接来电的问题
 
1.1 使用广播接收器 brocastreceiver
实现思路 :
静态注册监听android.intent.action.phone_state 的广播接收器 当手机的状态改变后将会触发 onreceive.
手机的状态分为call_state_ringing(响铃中),call_state_idle(空闲),call_state_offhook(忙音).
也就是说当你没有任何电话是,状态是 idle ,当接到电话时是 offhook ,电话结束后返回 idle 状态。
记录上一次的手机状态,如果的手机现在的空闲,上次的状态响铃中的话,就可以判断是未接来电.

<uses-permission android:name="android.permission.read_phone_state"/> 
<receiver android:name="com.example.phonestatedemo.receiver.phonestatereceiver"> 
   <intent-filter > 
      <action android:name="android.intent.action.phone_state"/> 
   </intent-filter> 
 </receiver> 

 

import android.content.broadcastreceiver; 
import android.content.context; 
import android.content.intent; 
import android.telephony.telephonymanager; 
import android.util.log; 
import android.widget.toast; 
 
public class phonestatereceiver extends broadcastreceiver { 
 
  private static int lastcallstate = telephonymanager.call_state_idle; 
   
  @override 
  public void onreceive(context arg0, intent arg1) { 
    string action = arg1.getaction(); 
    log.d("phonestatereceiver", action ); 
    telephonymanager telephonymanager = (telephonymanager) arg0 
        .getsystemservice(context.telephony_service); 
    int currentcallstate = telephonymanager.getcallstate(); 
    log.d("phonestatereceiver", "currentcallstate=" + currentcallstate ); 
    if (currentcallstate == telephonymanager.call_state_idle) {// 空闲 
      //todo  
    } else if (currentcallstate == telephonymanager.call_state_ringing) {// 响铃 
      //todo  
    } else if (currentcallstate == telephonymanager.call_state_offhook) {// 接听 
      //todo  
    } 
    if(lastcallstate == telephonymanager.call_state_ringing &&  
          currentcallstate == telephonymanager.call_state_idle){ 
      toast.maketext(arg0, "有未接来电", 1).show(); 
    } 
     
    lastcallstate = currentcallstate; 
 
  } 
 
} 
 

1.2  使用 phonestatelistener
实现思路 :
继承phonestatelistener后,当手机的状态改变后将会触发oncallstatechanged.手机的状态分为call_state_ringing(响铃中),call_state_idle(空闲),call_state_offhook(忙音).
也就是说当你没有任何电话是,状态是 idle ,当接到电话时是 offhook ,电话结束后返回 idle 状态。
记录上一次的手机状态,如果的手机现在的空闲,上次的状态响铃中的话,就可以判断是未接来电.
 
不足:现在的处理不能判断出是用户是否主动不接电话.

telephonymanager telephonymanager = (telephonymanager) this 
        .getsystemservice(context.telephony_service); 
    telephonymanager.listen(new callstatelistener(this), 
        phonestatelistener.listen_call_state); 
 

package com.example.phonestatedemo.listener; 
 
import android.content.context; 
import android.telephony.phonestatelistener; 
import android.telephony.telephonymanager; 
import android.util.log; 
import android.widget.toast; 
 
public class callstatelistener extends phonestatelistener { 
  private static int lastetstate = telephonymanager.call_state_idle; // 最后的状态 
  private context context; 
 
  public callstatelistener(context context) { 
    this.context = context; 
 
  } 
 
  @override 
  public void oncallstatechanged(int state, string incomingnumber) { 
    // todo auto-generated method stub 
    super.oncallstatechanged(state, incomingnumber); 
    log.d("callstatelistener", "oncallstatechanged state=" + state ); 
    // 如果当前状态为空闲,上次状态为响铃中的话,则破觚为认为是未接来电 
    if (lastetstate == telephonymanager.call_state_ringing 
        && state == telephonymanager.call_state_idle) { 
      //todo 
      toast.maketext(this.context, "callstatelistener 有未接来电", 1).show(); 
    } 
 
    lastetstate = state; 
 
  } 
 
}