当前位置:主页 > 移动开发 > Android代码 >

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

时间:2020-11-24 17:08:51 | 栏目:Android代码 | 点击:

这里主要是总结一下如何监听有未接来电的问题
 
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; 
 
  } 
 
} 

您可能感兴趣的文章:

相关文章