我正在开发一款我想成为的Android应用 能够打电话,但有一个非常精确的限制,这是 “打错了电话”。我想要的是,能够挂断电话 电话开始振铃的那一刻。
现在我能够知道手机何时开始尝试制作 打电话,但几秒钟内没有“振铃”活动 网络,这是我愿意做的。
我怎样才能停止这个确切的时刻?
答案 0 :(得分:2)
通过PhoneStateListener使用onCallStateChanged(),您只能检测到电话何时开始拨打电话以及何时拨打电话,但您无法确定何时拨打电话"开始了。我试过一次,看看下面的代码:
拨出电话时,拨出电话从IDLE开始到OFFHOOK,在拨号时拨打IDLE。 唯一的解决方法是在拨出电话开始后几秒钟内使用计时器挂断,但是,你永远不能保证手机会响铃。
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;
}
}
}
答案 1 :(得分:1)