5

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

4

3 回答 3

7

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

 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,检查一下

于 2013-02-06T05:07:10.950 回答
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);

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

于 2014-05-09T16:51:39.000 回答
0
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);
                    }
                }
            }
     }
}
于 2013-02-06T05:00:21.620 回答