Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an implementation which will allow you to retrieve the phone number if it is an incoming phone call as incomingNumber and also when the call is FINISHED - note the Handler() code.</p> <pre><code>private class PhoneCallListener extends PhoneStateListener { private boolean isPhoneCalling = false; @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 number"); if (isPhoneCalling) { Handler handler = new Handler(); //Put in delay because call log is not updated immediately when state changed // The dialler takes a little bit of time to write to it 500ms seems to be enough handler.postDelayed(new Runnable() { @Override public void run() { // get start of cursor Log.i("CallLogDetailsActivity", "Getting Log activity..."); String[] projection = new String[]{Calls.NUMBER}; Cursor cur = getContentResolver().query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc"); cur.moveToFirst(); String lastCallnumber = cur.getString(0); } },500); isPhoneCalling = false; } } } } </code></pre> <p>And then add and initialise the listener in your onCreate or onStartCommand code:</p> <pre><code>PhoneCallListener phoneListener = new PhoneCallListener(); TelephonyManager telephonyManager = (TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); </code></pre>
 

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