Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

android - Get phone number of present incoming and outgoing call

I am making an app in which I have to retrieve the phone number and I am able to retrieve it but my problem is that I am getting phone number of last call rather than present 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";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As suggested by Krutix, the call LOG, is a LOG of finished phone calls which is written to by the dialer app after a call is finished. As such you will not find which number is currently dialing in the content provider.

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.

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;
            }

        }
    }
}

And then add and initialise the listener in your onCreate or onStartCommand code:

    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener,
            PhoneStateListener.LISTEN_CALL_STATE);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...