Android中監(jiān)聽未接來電的2種方法
這里主要是總結(jié)一下如何監(jiān)聽有未接來電的問題
1.1 使用廣播接收器 BrocastReceiver
實現(xiàn)思路 :
靜態(tài)注冊監(jiān)聽android.intent.action.PHONE_STATE 的廣播接收器 當手機的狀態(tài)改變后將會觸發(fā) onReceive.
手機的狀態(tài)分為CALL_STATE_RINGING(響鈴中),CALL_STATE_IDLE(空閑),CALL_STATE_OFFHOOK(忙音).
也就是說當你沒有任何電話是,狀態(tài)是 IDLE ,當接到電話時是 OFFHOOK ,電話結(jié)束后返回 IDLE 狀態(tài)。
記錄上一次的手機狀態(tài),如果的手機現(xiàn)在的空閑,上次的狀態(tài)響鈴中的話,就可以判斷是未接來電.
<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
實現(xiàn)思路 :
繼承PhoneStateListener后,當手機的狀態(tài)改變后將會觸發(fā)onCallStateChanged.手機的狀態(tài)分為CALL_STATE_RINGING(響鈴中),CALL_STATE_IDLE(空閑),CALL_STATE_OFFHOOK(忙音).
也就是說當你沒有任何電話是,狀態(tài)是 IDLE ,當接到電話時是 OFFHOOK ,電話結(jié)束后返回 IDLE 狀態(tài)。
記錄上一次的手機狀態(tài),如果的手機現(xiàn)在的空閑,上次的狀態(tài)響鈴中的話,就可以判斷是未接來電.
不足:現(xiàn)在的處理不能判斷出是用戶是否主動不接電話.
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; // 最后的狀態(tài)
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 );
// 如果當前狀態(tài)為空閑,上次狀態(tài)為響鈴中的話,則破觚為認為是未接來電
if (lastetState == TelephonyManager.CALL_STATE_RINGING
&& state == TelephonyManager.CALL_STATE_IDLE) {
//TODO
Toast.makeText(this.context, "CallStateListener 有未接來電", 1).show();
}
lastetState = state;
}
}
相關(guān)文章
Flutter狀態(tài)管理Provider的使用示例詳解
這篇文章主要為大家介紹了Flutter狀態(tài)管理Provider的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android 多層嵌套后的 Fragment 懶加載實現(xiàn)示例
這篇文章主要介紹了Android 多層嵌套后的 Fragment 懶加載實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

