how to listen for a call disconnect on android?

狂风中的少年 提交于 2019-12-12 01:35:55

问题


I'm trying to figure out how to be notified when a call is disconnected - both incoming and outgoing. the intent is to launch something on call disconnect.

I looked at the telephnyManager page and i can see getCallState(), but looking at all the constants, i don't see anything like what i'm looking.

I'm guessing i need to set up a broadcast receiver - i'm just not sure what i'm listening to...

Thanks for helping!!


回答1:


Please try this link

it have some documentation regarding android.telephony Package

Hope this will help you




回答2:


in case someone is looking for the answer, here's the code (you never know when that link is gonna die!). This is not the complete code, but it's the code that will listen to a call change. What I didn't get when reading the documentation the first time was that getting a CALL_STATE_IDLE response, means that the connection that was there before is now idle - i.e., the call has been disconnected.

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

private class ListenToPhoneState extends PhoneStateListener {

        public void onCallStateChanged(int state, String incomingNumber) {
            Log.i("telephony-example", "State changed: " + stateName(state));
        }

        String stateName(int state) {
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE: return "Idle";
                case TelephonyManager.CALL_STATE_OFFHOOK: return "Off hook";
                case TelephonyManager.CALL_STATE_RINGING: return "Ringing";
            }
            return Integer.toString(state);
        }
    }


来源:https://stackoverflow.com/questions/23439842/how-to-listen-for-a-call-disconnect-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!