How to Fetch Owner Mobile number from SMS Inbox/Sent

六眼飞鱼酱① 提交于 2020-01-05 08:55:08

问题


I have tried to retrieve all information from SMS Inbox/Outbox of my device. But my mobile number i.e. Sender's mobile number does not seem to be stored. I am trying to get this number so that I can capture mobile number from the OUTBOX and allow user to use it to register on my App, considering it to be near accurate mobile number that the user is using on the device. So that they don't have to type the mobile in my app for registration.

Please let me know if there is any way to find mobile number from the SMS INBOX/SENT

Code here:

public void readSMS (Activity MainActivity) 
{
    Uri inSMSUri = Uri.parse ("content://sms/inbox");
    System.out.println ( "the URI :: " + inSMSUri);

    Cursor c = MainActivity.getContentResolver().query(inSMSUri, null, null, null, "date desc");
    if (c!= null) 
    {

        StringBuilder sb = new StringBuilder ();
        while (c.moveToNext ()) {
            System.out.println ("content:" + c.getString (c.getColumnIndex ("body")));

            for(int i=0; i<c.getColumnCount();i++)
            {
                System.out.println("SMS:INBOX: c getColumnName(i):: "+c.getColumnName(i) + 
                " c.getString(i):: "+c.getString(i));
            }

        }
        c.close ();


        Uri outSMSUri = Uri.parse ("content://sms/sent");

        c = MainActivity.getContentResolver().query(outSMSUri, null, null, null, "date desc");
        if (c!= null) 
        {

            while (c.moveToNext ()) {
                System.out.println ("content:" + c.getString (c.getColumnIndex ("body")));

                for(int i=0; i<c.getColumnCount();i++)
                {
                    System.out.println("SMS:SENT: c getColumnName(i):: "+c.getColumnName(i) + 
                    " c.getString(i):: "+c.getString(i));
                }

            }

        }

    }
}

But SENDER Mobile number column NOT found:

SMS INBOX is generating this output:

content:Hello! You are roaming on Vodafone Maharashtra

SMS:INBOX: c getColumnName(i):: _id c.getString(i):: 9

SMS:INBOX: c getColumnName(i):: thread_id c.getString(i):: 3

SMS:INBOX: c getColumnName(i):: address c.getString(i):: VM-VDFONE

SMS:INBOX: c getColumnName(i):: m_size c.getString(i):: 297

SMS:INBOX: c getColumnName(i):: person c.getString(i):: 0

SMS:INBOX: c getColumnName(i):: date c.getString(i):: 1381599923412

SMS:INBOX: c getColumnName(i):: date_sent c.getString(i):: 1381599913000

SMS:INBOX: c getColumnName(i):: protocol c.getString(i):: 0

SMS:INBOX: c getColumnName(i):: read c.getString(i):: 1

SMS:INBOX: c getColumnName(i):: status c.getString(i):: -1

SMS:INBOX: c getColumnName(i):: type c.getString(i):: 1

SMS:INBOX: c getColumnName(i):: reply_path_present c.getString(i):: 0

SMS:INBOX: c getColumnName(i):: subject c.getString(i):: null

SMS:INBOX: c getColumnName(i):: body c.getString(i):: Hello! You are roaming on Vodafone Maharashtra

SMS:INBOX: c getColumnName(i):: service_center c.getString(i):: +919820005556

SMS:INBOX: c getColumnName(i):: locked c.getString(i):: 0

SMS:INBOX: c getColumnName(i):: sim_id c.getString(i):: 1

SMS:INBOX: c getColumnName(i):: error_code c.getString(i):: 0

SMS:INBOX: c getColumnName(i):: seen c.getString(i):: 1

SMS:INBOX: c getColumnName(i):: ipmsg_id c.getString(i):: 0

But SENDER Mobile number column NOT found:

SMS:SENT: c getColumnName(i):: _id c.getString(i):: 1783

SMS:SENT: c getColumnName(i):: thread_id c.getString(i):: 26

SMS:SENT: c getColumnName(i):: address c.getString(i):: 1909

SMS:SENT: c getColumnName(i):: m_size c.getString(i):: null

SMS:SENT: c getColumnName(i):: person c.getString(i):: 0

SMS:SENT: c getColumnName(i):: date c.getString(i):: 1394009796560

SMS:SENT: c getColumnName(i):: error_code c.getString(i):: 0

SMS:SENT: c getColumnName(i):: seen c.getString(i):: 1

SMS:SENT: c getColumnName(i):: ipmsg_id c.getString(i):: 0

SMS:SENT: c getColumnName(i):: m_size c.getString(i):: 21

SMS:SENT: c getColumnName(i):: person c.getString(i):: 0

SMS:SENT: c getColumnName(i):: date c.getString(i):: 1392004530098

SMS:SENT: c getColumnName(i):: date_sent c.getString(i):: 0

SMS:SENT: c getColumnName(i):: protocol c.getString(i):: null

SMS:SENT: c getColumnName(i):: read c.getString(i):: 1

SMS:SENT: c getColumnName(i):: status c.getString(i):: -1

SMS:SENT: c getColumnName(i):: type c.getString(i):: 2

SMS:SENT: c getColumnName(i):: reply_path_present c.getString(i):: null

SMS:SENT: c getColumnName(i):: subject c.getString(i):: null

SMS:SENT: c getColumnName(i):: body c.getString(i):: Take care

SMS:SENT: c getColumnName(i):: service_center c.getString(i):: null

SMS:SENT: c getColumnName(i):: locked c.getString(i):: 0

SMS:SENT: c getColumnName(i):: sim_id c.getString(i):: 1

SMS:SENT: c getColumnName(i):: error_code c.getString(i):: 0

SMS:SENT: c getColumnName(i):: ipmsg_id c.getString(i):: 0

回答1:


You can try the following ways to get the mobile number:

  1. TelephonyManager - Will not work for most of the numbers TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); final String contactNumber = telephonyManager.getLine1Number();

  2. AccountManager - Fetch number from accounts like WhatsApp and Telegram AccountManager accountManager = AccountManager.get(context); Account[] accounts = accountManager.getAccounts(); String contactNumber = null; if (accounts.length > 0) { for (Account acc : accounts) { if ("com.whatsapp".equals(acc.type)) { if (!acc.name.matches(".[a-zA-Z]+.")) { contactNumber = acc.name; } } if (contactNumber == null && "org.telegram.messenger.account".equals(acc.type)) { if (!acc.name.matches(".[a-zA-Z]+.")) { contactNumber = acc.name; } } } }

  3. Integrate with Sms API like https://www.twilio.com/ Ask user for number and send sms to the user's number with some authentication code. Either ask user to input the authentication code or the app can directly read the incoming message, parse the authentication code and validate. It might not work with full DND registered numbers from India.

  4. Missed Call!!! Integrate with service like http://dial2verify.in/ Ask user for phone number, make a call to dial2verify, it will return a phone number, ask user to give a missed call to the number. Once the missed call is received, dial2verify will make a call to your server with validate number details.



回答2:


content:// has not worked well for me in the past. It seems that this changes based on the phone that the person is using.

I personally use this to get the phone number of the user:

TelephonyManager tMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

ownerPhoneNumber = tMgr.getLine1Number();

If you NEED to do it that way though, I use:

Uri uri = Uri.parse("content://sms/");

Cursor cursor = contentResolver.query(uri, null, null, null, null);
cursor.moveToFirst();

String type = cursor.getString(cursor.getColumnIndex("type"));
if (type.equals("2"))
{
     //An outgoing SMS was sent.
}

You can grab the phone number using cursor.getString(cursor.getColumnIndex("")) (Although I forgot what column goes inside there for phone number. You can try a loop like you did above to see what values you receive.



来源:https://stackoverflow.com/questions/23946071/how-to-fetch-owner-mobile-number-from-sms-inbox-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!