1

我试图从我设备的 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
4

2 回答 2

0

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

  1. TelephonyManager - 不适用于大多数号码 TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); 最终字符串 contactNumber = telephonyManager.getLine1Number();

  2. AccountManager - 从 WhatsApp 和 Telegram AccountManager accountManager = AccountManager.get(context) 等帐户中获取号码;帐户[] 帐户 = accountManager.getAccounts(); 字符串联系人号码 = 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. 与https://www.twilio.com/等短信 API 集成 向用户询问号码并使用一些身份验证码向用户的号码发送短信。要么要求用户输入验证码,要么应用程序可以直接读取传入的消息,解析验证码并进行验证。它可能不适用于来自印度的完整免打扰注册号码。

  4. 未接来电!!!与http://dial2verify.in/等服务集成 询问用户电话号码,拨打 dial2verify,它会返回一个电话号码,要求用户给该号码打个未接电话。一旦收到未接来电,dial2verify 将使用验证号码详细信息向您的服务器发出呼叫。
于 2014-05-30T18:21:20.780 回答
-1

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(""))(尽管我忘记了电话号码在里面的哪一列。您可以尝试像上面那样循环以查看收到的值。

于 2014-05-30T02:37:51.740 回答