PhoneStateListener के माध्यम से onCallStateChanged() का उपयोग आप केवल पता लगा सकते हैं फोन शुरू होता है आउटगोइंग कॉल के बनाने के लिए और जब आउटगोइंग कॉल अप hunged है जब लेकिन आप निर्धारित नहीं कर सकता जब में एक "रिंगिंग" शुरू हो गया है। मैंने एक बार कोशिश की, नीचे दिए गए कोड को देखें:
लापता होने पर आईडीएलई पर डायल किए जाने पर एक आउटगोइंग कॉल आईडीएलई से ऑफफोक से शुरू होती है। आउटगोइंग कॉल शुरू होने के बाद लटकने के लिए टाइमर का उपयोग करना और कुछ ही सेकंड गुजरने का एकमात्र कामकाज है, लेकिन फिर, आपको कभी गारंटी नहीं है कि फोन बजने लगेगा।
public abstract class PhoneCallReceiver extends BroadcastReceiver {
static CallStartEndDetector listener;
@Override
public void onReceive(Context context, Intent intent) {
savedContext = context;
if(listener == null){
listener = new CallStartEndDetector();
}
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public class CallStartEndDetector extends PhoneStateListener {
int lastState = TelephonyManager.CALL_STATE_IDLE;
boolean isIncoming;
public PhonecallStartEndDetector() {}
//Incoming call- IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when hung up
//Outgoing call- from IDLE to OFFHOOK when dialed out, to IDLE when hunged up
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if(lastState == state){
//No change
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
//incoming call started
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing down on them
if(lastState != TelephonyManager.CALL_STATE_RINGING){
isIncoming = false;
//outgoing call started
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//End of call(Idle). The type depends on the previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING){
// missed call
}
else if(isIncoming){
//incoming call ended
}
else{
//outgoing call ended
}
break;
}
lastState = state;
}
}
}
स्रोत
2014-03-19 02:44:29