获取特定电话号码的短信

时间:2013-02-06 04:47:20

标签: android

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

3 个答案:

答案 0 :(得分:6)

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

 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

答案 1 :(得分: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);

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

答案 2 :(得分: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);
                    }
                }
            }
     }
}