-2

我想从特定号码读取短信然后删除而不将其保存在收件箱中。下面是我读取短信的代码,但此代码将短信保存在收件箱中。阅读后如何从特定号码中删除短信?

public class SmsReceiver extends BroadcastReceiver 
{
public static  String SMSstr;
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 ("15555215556".equals(phNum))
            {
                // delete code goes here

                Intent l = new Intent(context,AgAppMenu.class);
                l.putExtra("msg",str);
                l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(l);
                Toast.makeText(context, "SucessFull Login", Toast.LENGTH_SHORT).show();
            }
        }


         <receiver android:name=".SmsReceiver" android:enabled="true"> 
     <!--       <intent-filter >-->
            <intent-filter android:priority="1000"> 
            <action android:name=
                "android.provider.Telephony.SMS_RECEIVED" /> 
       </intent-filter> 
    </receiver>
4

2 回答 2

2

如果您想删除消息,只需abortBroadcast()在检查所需来源后调用!

于 2013-05-23T11:16:20.733 回答
1

试试下面的代码,

String specificPhoneNumber = "9898989898";

String addr = msg[i].getOriginatingAddress().trim();  // to get the SMS Number

if ( addr.trim().equals ( specificPhoneNumber.trim() )
{
      // delete SMS
      Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null);
      String pid = c.getString(1);
      String uri = "content://sms/conversations/" + pid; 
      getContentResolver().delete(Uri.parse(uri), null, null);
}
于 2012-09-13T11:25:06.377 回答