如何从SMS收件箱/已发送中获取所有者手机号码

时间:2014-05-30 02:24:21

标签: java android mobile sms owner

我尝试从设备的SMS收件箱/发件箱中检索所有信息。但我的手机号码即发件人的手机号码似乎没有存储。我试图获取这个号码,以便我可以从OUTBOX中捕获手机号码,并允许用户在我的应用程序上注册,考虑到它接近用户在设备上使用的准确手机号码。这样他们就不必在我的应用程序中键入移动设备进行注册。

如果有任何方法可以从SMS INBOX / SENT

找到手机号码,请告诉我

代码在这里:

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

            }

        }

    }
}

但找不到SENDER手机号码栏:

SMS INBOX正在生成此输出:

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

2 个答案:

答案 0 :(得分:0)

过去,{p> content://对我来说效果不佳。这似乎是根据该人正在使用的手机而改变的。

我个人用它来获取用户的电话号码:

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

ownerPhoneNumber = tMgr.getLine1Number();

如果您需要这样做,我会使用:

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

您可以使用cursor.getString(cursor.getColumnIndex(""))获取电话号码(虽然我忘记了哪个列里面有电话号码。您可以像上面那样尝试循环,看看您收到了什么值。

答案 1 :(得分:0)

您可以尝试以下方式获取手机号码:

  1. TelephonyManager - 对大多数数字不起作用     TelephonyManager telephonyManager =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);     final String contactNumber = telephonyManager.getLine1Number();

  2. AccountManager - 从WhatsApp和Telegram等帐户中获取号码 AccountManager accountManager = AccountManager.get(context);     Account [] accounts = accountManager.getAccounts();     String contactNumber = null;     if(accounts.length&gt; 0){         for(帐户acc:accounts){             if(&#34; com.whatsapp&#34; .equals(acc.type)){                 if(!acc.name.matches(&#34;。 [a-zA-Z] +。&#34;)){                     contactNumber = acc.name;                 }             }             if(contactNumber == null&amp;&amp;&#34; org.telegram.messenger.account&#34; .equals(acc.type)){                 if(!acc.name.matches(&#34;。 [a-zA-Z] +。&#34;)){                     contactNumber = acc.name;                 }             }         }     }

  3. https://www.twilio.com/等Sms API集成 询问用户号码并使用某些验证码将短信发送到用户的号码。要么用户输入验证码,要么应用程序可以直接读取收到的邮件,解析验证码并验证。 它可能不适用于来自印度的完整DND注册号码。

  4. 错过了电话!!! 与http://dial2verify.in/等服务集成 询问用户的电话号码,拨打电话拨打2,它会返回一个电话号码,要求用户拨打未接来电号码。收到未接来电后,dial2verify将通过验证号码详细信息拨打您的服务器。