0

此代码仅删除来自特定号码的最后一条消息,而不是来自特定号码的所有消息。如何删除所有消息?

如果我在运行应用程序之前发送 3 条消息,那么在收到新消息时运行应用程序将停止应用程序。如果我手动删除收件箱中的所有消息,然后在启动应用程序后从特定号码发送短信,那么它工作正常。那么,我应该如何修改此代码以检查收件箱中具有特定编号的先前消息并将它们全部删除。

public class SmsReceiver extends BroadcastReceiver {    
String specificPhoneNumber = "15555215554";

public void onReceive(Context context, Intent intent) {

    abortBroadcast();
    //---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);                   
        }}}
4

0 回答 0