2012-09-13 55 views
-2

我想從特定號碼讀取短信,然後刪除而不將其保存在收件箱中。下面是我的代碼,它讀取短信,但此代碼將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> 
+0

請幫我me –

回答

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); 
} 
+0

我如何在我的代碼中插入此代碼? PLZ編輯我的代碼plzz –

+0

插入我的if語句後代碼if(「15555215556」.equals(phNum))' – Lucifer

+0

此代碼插入content.startActivity(l)之後還是之前? –

2

如果你想刪除的郵件只需調用abortBroadcast()您檢查所需的信號源後!