【问题标题】:Get SMS of specific phone number获取特定电话号码的短信
【发布时间】:2013-02-06 04:47:19
【问题描述】:

我想知道,如何以编程方式读取特定号码的短信? 我知道如何使用内容提供者阅读短信,但不确定我应该使用“人”栏还是“地址”栏或完全不同的方式 请帮忙 谢谢

【问题讨论】:

    标签: android


    【解决方案1】:

    它将列出来自指定号码的消息。

     Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
             Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, null, null, null);
             startManagingCursor(cursor1);
             String[] columns = new String[] { "address", "person", "date", "body","type" };
             if (cursor1.getCount() > 0) {
                String count = Integer.toString(cursor1.getCount());
                while (cursor1.moveToNext()){
                    String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
    
                    if(address.equalsIgnoreCase("number")){ //put your number here
                         String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
                         String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
                         String body = cursor1.getString(cursor1.getColumnIndex(columns[3]));
                         String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
    
                         Log.d("*******", "body="+body);
    
                   }
    
    
                 }
             }
    

    但我遇到了"content://mms-sms/conversations/",我认为它会在thread_id的帮助下直接返回特定号码的整个对话,检查this

    【讨论】:

    • 您好,感谢您的回答,但这并不能回答我的问题。在这里,您说我如何准备收到的短信。我的问题是,我有一个特定的号码“xxxxxxxx”,我怎样才能从收件箱中获取这个号码发送的所有消息。因此,与 onReceive 没有任何关系,因为它是一项读取收件箱短信并通过它们尝试查找特定电话号码的短信的活动
    • 我闻到你有双卡,现在你想获取所有特定号码发送的消息,但这句话不清楚,你在做什么how can I get all the messages from the inbox that were sent by this number.???因为收件箱包含未发送的已接收消息,而您希望从收件箱发送消息,所以它没有任何意义。
    • 我指的不是“发送”消息。我指的是“收到的消息”。如果您仔细阅读我上面的评论,您会发现我询问的是“BY”这个号码而不是“To”发送的消息。换句话说,我正在寻找从这个号码收到的消息。我不想收听当前收到的消息,而是收听过去收到的消息。例如,我想知道昨天收到的来自号码 xxxx 的消息。
    • 现在这就是我要找的! :) 谢谢,接受!
    • 电话号码是否标准化?这里不应该用android.telephony.PhoneNumberUtils.compare吗?
    【解决方案2】:

    您可以使用 SelectionArgs 来提高效率:

    String[] phoneNumber = new String[] { "+18839494492" }; //the wanted phone number
    Cursor cursor1 = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, "address=?", phoneNumber, null);
    

    通过此更改,您只能从想要的号码中获取短信,而无需抓取所有收到的短信。

    【讨论】:

    • 这会规范电话号码吗?例如,如果搜索查询具有国际前缀怎么办?
    【解决方案3】:
    public class SmsReceiver extends BroadcastReceiver {
    
        String specificPhoneNumber = "No you want";
    
        public void onReceive(Context context, Intent intent) 
        {
                //---get the SMS message passed in---
                Bundle bundle = intent.getExtras();        
                SmsMessage[] msgs = null;
                String str = "";  
    
                if (bundle != null)
                {         
                    //---retrieve the SMS message received---
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];            
                    for (int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        String phNum = msgs[i].getOriginatingAddress();  
                        str += msgs[i].getMessageBody().toString();
                 if (specificPhoneNumber.equals(phNum))
    
                {
                    Uri uri = Uri.parse("content://sms/inbox");
    
                    ContentResolver contentResolver = context.getContentResolver();
    
                    String where = "address="+phNum;
                    Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null,
                                      null);
    
                    while (cursor.moveToNext()) {
    
                        long thread_id = cursor.getLong(1);
                        where = "thread_id="+thread_id;
                        Uri thread = Uri.parse("content://sms/inbox");
                        context.getContentResolver().delete(thread, where, null);
    
                    }
                            Intent l = new Intent(context,AgAppMenu.class);                  
                            l.putExtra("msg",str);                   
                            l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(l);
                        }
                    }
                }
         }
    }
    

    【讨论】:

    • 这会规范电话号码吗?例如,如果搜索查询具有国际前缀怎么办?
    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2012-01-28
    • 1970-01-01
    相关资源
    最近更新 更多