5

プログラムで特定の番号の SMS メッセージを読むにはどうすればよいですか? コンテンツ プロバイダーを使用して SMS を読む方法は知っていますが、「人物」列を使用するべきか、「アドレス」列を使用するべきか、またはまったく別の方法を使用すべきかわかりません。助けてくださいありがとう

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);

この変更により、必要な番号からのみ SMS を取得し、受信したすべての SMS をクロールする必要がなくなります。

于 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 に答える