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)文章
kotlin協(xié)程之coroutineScope函數(shù)使用詳解
這篇文章主要為大家介紹了kotlin協(xié)程之coroutineScope函數(shù)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09Android仿淘寶頭條基于TextView實現(xiàn)上下滾動通知效果
這篇文章主要介紹了Android TextView實現(xiàn)上下滾動通知效果,需要的朋友可以參考下2017-03-03Kotlin中常見內(nèi)聯(lián)擴展函數(shù)的使用方法教程
在Kotlin中,使用inline修飾符標記內(nèi)聯(lián)函數(shù),既會影響到函數(shù)本身, 也影響到傳遞給它的Lambda表達式,這兩者都會被內(nèi)聯(lián)到調(diào)用處。下面這篇文章主要給大家介紹了關(guān)于Kotlin中常見內(nèi)聯(lián)擴展函數(shù)的使用方法,需要的朋友可以參考下。2017-12-12android中px和dp,px和sp之間的轉(zhuǎn)換方法
在Android開發(fā)中dp和px,sp和px之間的轉(zhuǎn)換時必不可少的。下面腳本之家小編給大家?guī)砹薬ndroid中px和dp,px和sp之間的轉(zhuǎn)換方法,感興趣的朋友一起看看吧2018-06-06深入Understanding Android ContentProvider詳解
本篇文章是對Android ContentProvider進行了詳細的分析介紹,需要的朋友參考下2013-05-05AndroidStudio 配置 AspectJ 環(huán)境實現(xiàn)AOP的方法
本篇文章主要介紹了AndroidStudio 配置 AspectJ 環(huán)境實現(xiàn)AOP的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02