hi guys,

currently i'm always getting the previously dialed number. but what i
need is current dialed number.

this is my code the method to retrieve number is quoted from a another
developer who helped me but it always gives the previous number. need
help urgently

public class CallListener extends BroadcastReceiver {
        private Context context;
        // Intent intentttt = null;
        String phonenbr;

        @Override
        public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                this.context = context;
                TelephonyManager telManager = (TelephonyManager)
context
                                .getSystemService(Context.TELEPHONY_SERVICE);
                telManager.listen(new StateListener(),
                                PhoneStateListener.LISTEN_CALL_STATE);

        }

        class StateListener extends PhoneStateListener {
                @Override
                public void onCallStateChanged(int state, String
incomingNumber) {
                        // TODO Auto-generated method stub
                        // super.onCallStateChanged(state,
incomingNumber);
                        switch (state) {
                        case TelephonyManager.CALL_STATE_RINGING:
                                Log.d("DEBUG", "RINGING");
                                break;
                        case TelephonyManager.CALL_STATE_IDLE:
                                //
BroadcastReceiver.this.clearAbortBroadcast();
                                //context.unregisterReceiver(this.);
                                System.exit(0);
                                break;
                        case TelephonyManager.CALL_STATE_OFFHOOK:
                                phonenbr =
getLastCallLogEntry(context);
                                break;
                        }
                }
        }

        private String getLastCallLogEntry(Context context) {
                String[] projection = new String[] { BaseColumns._ID,
                                CallLog.Calls.NUMBER,
CallLog.Calls.TYPE };
                ContentResolver resolver =
context.getContentResolver();
                Cursor cur = resolver.query(CallLog.Calls.CONTENT_URI,
projection,
                                null, null,
CallLog.Calls.DEFAULT_SORT_ORDER);
                int numberColumn =
cur.getColumnIndex(CallLog.Calls.NUMBER);
                int typeColumn =
cur.getColumnIndex(CallLog.Calls.TYPE);

                if (!cur.moveToNext()) {
                        cur.close();
                        return "";
                }
                String number = cur.getString(numberColumn);
                String type = cur.getString(typeColumn);
                String dir = null;
                try {
                        int dircode = Integer.parseInt(type);
                        switch (dircode) {
                        case CallLog.Calls.OUTGOING_TYPE:
                                dir = "OUTGOING";
                                break;

                        case CallLog.Calls.INCOMING_TYPE:
                                dir = "INCOMING";
                                break;

                        case CallLog.Calls.MISSED_TYPE:
                                dir = "MISSED";
                                break;
                        }
                } catch (NumberFormatException ex) {
                }
                if (dir == null)
                        dir = "Unknown, code: " + type;
                cur.close();
                return number;

        }

}
regards,
Mike

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to