Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you need to start your PhoneCallListener right before you call, pass the number you are going to call to the PhoneCallListener and then start the callIntent.</p> <p>In your PhoneListener, you can check if the number matches with the number you passed from your Activity. If true, restart your activitry, else do nothing.</p> <p>EDIT:</p> <pre><code>public void imgbtnCallPhone_Click(View view) { EditText txtBusinessPhone = (EditText) findViewById(R.id.txtPhone); // Get your PhoneCallListener and pass the number PhoneCallListener mPhoneListener = new PhoneCallListener(); mPhoneListener.yourActivity = true; // start listening TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); try { final Intent callIntent = new Intent(android.content.Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:"+ txtBusinessPhone.getText())); startActivity(callIntent); } catch (ActivityNotFoundException activityException) { //Log.e("Calling a Phone Number", "Call failed", activityException); } } </code></pre> <p>Your PhoneCallListener:</p> <pre><code>private class PhoneCallListener extends PhoneStateListener { private boolean isPhoneCalling = false; public Boolean yourActivity = false; String LOG_TAG = "LOGGING 123"; @Override public void onCallStateChanged(int state, String incomingNumber) { if (TelephonyManager.CALL_STATE_RINGING == state) { // phone ringing //Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); } if (TelephonyManager.CALL_STATE_OFFHOOK == state) { // active //Log.i(LOG_TAG, "OFFHOOK"); isPhoneCalling = true; } if (TelephonyManager.CALL_STATE_IDLE == state) { // run when class initial and phone call ended, // need detect flag from CALL_STATE_OFFHOOK //Log.i(LOG_TAG, "IDLE"); if (isPhoneCalling &amp;&amp; yourActivity) { //Log.i(LOG_TAG, "restart app"); // restart app Intent i = getBaseContext().getPackageManager() .getLaunchIntentForPackage( getBaseContext().getPackageName()); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); yourActivity = false; isPhoneCalling = false; } } } } </code></pre> <p>I didn't test it, so it may contains some errors.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload