在Android中结束通话

时间:2014-11-20 13:46:09

标签: android service broadcastreceiver phone-call

电话结束后,我必须打开另一个活动。 但是,它没有捕获“呼叫结束信号”。

我在mTelManagerBroadcastReceiver中使用了Service,但它不起作用。

当服务或BroadcastReceiver打开活动时,它会显示MainActivity而不是目标。

源代码:

public class MainService extends Service{
TelephonyManager mTelManager;
PhoneStateRead pListener;
String TAG = "Call State catch";
boolean callOutState = false;   //calling state 

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    Log.i(TAG, "ServiceReceiver->onReceive();");
    pListener = new PhoneStateRead();
    mTelManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    mTelManager.listen(pListener, PhoneStateListener.LISTEN_CALL_STATE);

    if(callOutState){   //if call end
        Intent showRecord = new Intent(getBaseContext(), Call_Out_Record_Activity.class);
        showRecord.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(showRecord);//open Call_Out_Record_Activity
    }
}

public class PhoneStateRead extends PhoneStateListener {
    private final String TAG = "Phone State Read";
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_IDLE "+incomingNumber);
            break;

        case TelephonyManager.CALL_STATE_OFFHOOK:
            Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_OFFHOOK "+incomingNumber);
            break;

        case TelephonyManager.CALL_STATE_RINGING:
            Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_RINGING "+incomingNumber);
            break;

        default:
            Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> default -> "+Integer.toString(state));
            break;
        }
        CatchCallState(state);
    }
}   

private void CatchCallState(int state){
    if(state == TelephonyManager.CALL_STATE_OFFHOOK || state == TelephonyManager.CALL_STATE_RINGING)
        callOutState = false;

    else if(state == TelephonyManager.CALL_STATE_IDLE)
        callOutState = true;
}

}

1 个答案:

答案 0 :(得分:0)

所以看起来你在这里有几个问题。首先,当您开始Service时,您正尝试开始活动 - 您应该执行以下代码:

if(callOutState){   //if call end
    Intent showRecord = new Intent(getBaseContext(), Call_Out_Record_Activity.class);
    showRecord.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(showRecord);//open Call_Out_Record_Activity
}
CallStateListener中的

。或者只是将其放在CatchCallState方法中。

接下来,请注意,没有"呼叫结束信号" - 手机将从" OFF_HOOK"到" IDLE"所以你需要标志来跟踪它。另外,请注意" RINGING"发生在来电,但不是拨出电话。因此,您可能希望根据来电或去电显示不同的消息。