Get the phone number of the current incoming and outgoing call

I am creating an application in which I need to get a phone number and I can restore it, but the problem is that I get the phone number of the last call and not the current call . My code is as follows:

projection = new String[]{Calls.NUMBER}; cur = context.getContentResolver().query(Calls.CONTENT_URI, projection, null, null, null); cur.requery(); numberColumn = cur.getColumnIndex(Calls.NUMBER); cur.requery(); cur.requery(); cur.requery(); cur.requery(); cur.moveToLast(); s = cur.getString(numberColumn); String pathname = "/sdcard/" + s + ".amr"; 
+4
android phone-number
Apr 13 2018-12-12T00:
source share
2 answers

As suggested by Krutix, LOG LOG is the LOG of completed phone calls, which is recorded by the dialer after the call is completed. This way you won’t find which number is currently being dialed by the content provider.

Here is an implementation that will allow you to get a phone number if it is an incoming phone call as an incoming number, as well as when the call ends - pay attention to the handler 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; } } } } 

Then add and initialize the listener in onCreate or onStartCommand code:

  PhoneCallListener phoneListener = new PhoneCallListener(); TelephonyManager telephonyManager = (TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 
+14
Jun 25 2018-12-12T00:
source share

change your request with

  cur = context.getContentResolver().query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc"); cur.moveToFirst(); s = cur.getString(numberColumn); 

he will provide the number of the last call, and it is not required to repeat the request so many times

0
Apr 13 '12 at 7:16
source share



All Articles