如何读取短信和删除而不保存在收件箱中

时间:2012-09-13 11:14:07

标签: android sms

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

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>

2 个答案:

答案 0 :(得分:2)

如果您想要删除邮件,请在检查所需来源后立即致电abortBroadcast()

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