【问题标题】:How to Fetch Owner Mobile number from SMS Inbox/Sent如何从短信收件箱/发送中获取所有者手机号码
【发布时间】:2014-05-30 02:24:21
【问题描述】:

我已尝试从设备的 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

【问题讨论】:

    标签: java android mobile sms owner


    【解决方案1】:

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

    1. TelephonyManager - 不适用于大多数号码 TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); final String contactNumber = telephonyManager.getLine1Number();

    2. AccountManager - 从 WhatsApp 和 Telegram 等帐户中获取号码 AccountManager accountManager = AccountManager.get(context); 帐户[] 帐户 = accountManager.getAccounts(); 字符串联系人号码 = null; 如果(accounts.length > 0){ 对于(帐户帐户:帐户){ 如果(“com.whatsapp”.equals(acc.type)){ if (!acc.name.matches(".[a-zA-Z]+.")) { 联系人号码 = acc.name; } } if (contactNumber == null && "org.telegram.messenger.account".equals(acc.type)) { if (!acc.name.matches(".[a-zA-Z]+.")) { 联系人号码 = acc.name; } } } }

    3. 与短信 API 集成,例如 https://www.twilio.com/ 向用户询问号码并使用一些验证码向用户的号码发送短信。要么要求用户输入验证码,要么应用程序可以直接读取传入的消息,解析验证码并进行验证。 它可能不适用于来自印度的完整免打扰注册号码。

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

    【讨论】:

    • 您好 Devashish,1. 我已经实现了电话管理器,但并非所有的 SIM 卡都存储手机号码。 2.我还实现了帐户经理代码来获取帐户名称(whatsapp 不再将电话号码存储在帐户中)对于 3 和 4 选项:我正在尝试自动获取电话号码,从而避免要求用户输入整个10 位数字。 3 和 4 将在我获取号码​​后出现,但在这个阶段我没有自动获取获取方法。要求用户输入手机号码是最后一个选项。
    • @suren 目前还没有保证方法,这就是whatsapp之类的应用程序要求用户输入数字的原因。使用 1、2 和 4 将有助于节省短信费用。
    【解决方案2】:

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

    【讨论】:

    • 嗨 MJ,我已经完成了循环 (for(int i=0; i
    • 您说得对,似乎没有通用的方法来获取发件人的电话号码。虽然,很容易找到接收者的电话号码......但这对你的情况没有多大帮助。这是另一个 SO 问题的解决方案:stackoverflow.com/a/5134725/533049
    猜你喜欢
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    相关资源
    最近更新 更多