Ending a phone call in Android

317 Views Asked by At

After a phone call ends, I must open another Activity. However, it doesn't catch the "call end signal".

I've used mTelManager in BroadcastReceiver and Service, but it doesn't work.

When Activity is opened by a Service or BroadcastReceiver, it shows the MainActivity and not the targeted one instead.

Source code:

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

There are 1 best solutions below

0
On

So it looks like you have a couple problems here. First, you are trying to start your activity when you start your Service - you should do this code:

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
}

in the CallStateListener. Or just put it in your CatchCallState method.

Next, notice that there is not a "call end signal" - the phone will go from "OFF_HOOK" to "IDLE" so you need flags to track that. Also, notice that "RINGING" happens on incoming calls, but not outgoing calls. So you may want to display different messages depending on incoming or outgoing calls.