服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android中监听未接来电的2种方法

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

2021-03-18 15:07Android开发网 Android

这篇文章主要介绍了Android中监听未接来电的2种方法,本文讲解了使用广播接收器 BrocastReceiver和使用 PhoneStateListener二种方法,需要的朋友可以参考下

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

?
1
2
3
4
5
6
<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>

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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 状态。
记录上一次的手机状态,如果的手机现在的空闲,上次的状态响铃中的话,就可以判断是未接来电.
 
不足:现在的处理不能判断出是用户是否主动不接电话.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
 
  }
 
}

延伸 · 阅读

精彩推荐