wutzebaer
wutzebaer

Reputation: 14875

Android get sms receiver-number from inbox

I'm trying to receive the phone number of the device,

but

tMgr.getLine1Number();

is not reliable

now I'm trying to retrieve the number from the sms in in or sent box

Uri uriSMSURI = Uri.parse("content://sms/sent");
        Cursor cur = ctx.getContentResolver().query(uriSMSURI, null, null, null, null);
        String[] columnNames = cur.getColumnNames();
        if (cur.moveToFirst())
            do {
                Log.d("sms reciever",cur.getString(2));

            } while (cur.moveToNext());
        cur.close();

but on inbox column 2 is the sender, and in sentbox it is the reciver, any ideas how to retrieve the number of the current phone from the sms?

Upvotes: 1

Views: 383

Answers (1)

mertaydin
mertaydin

Reputation: 2244

This must help you

TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
  mPhoneNumber = tMgr.getLine1Number();

with READ_PHONE_STATE permission.

And I think there isn't best way to do this. Because this is dependening some conditions... Phone numbers may not stored in sim cards in everywhere, every location. You must ask it user to type his/her number and store it.

Upvotes: 1

Related Questions