2012-04-12 56 views
0

請告訴我如何刪除我在onReceive方法中捕獲的相同消息。實際上,我希望當onReceive方法收到按摩時,它首先使用關閉和使用後它刪除,但我怎麼不知道,請幫助我的情況下。 我正在使用的代碼也用這個線程標記。如何刪除在onReceive方法上捕獲的相同短信

@Override 
    public void onReceive(Context context,final Intent intents){ 
    if (intents.getAction().equals(ConstantClass.SMS_RECEIVED)) { 
    new Thread(){ 
    Context context; 
    Thread Set(Context ctx){ 
    this.context=ctx; 
    return this; 
    } 
    public void run(){ 
    try{ 
    Bundle bundle = intents.getExtras();    
    if (bundle != null) { 
    Object[] pdus = (Object[]) bundle.get("pdus"); 
    SmsMessage[] messages = new SmsMessage[pdus.length]; 
    for (int i = 0; i < pdus.length; i++) 
    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
    String msg=null; 
    String address = null; 
    for (SmsMessage message : messages) { 
    msg = message.getMessageBody(); 
    address = message.getOriginatingAddress(); 
    } 
    dba.Open(); 
    int id = dba.getCordiId(address); 
    int count = dba.getDeviceCount(ConstantClass.dbName[1]); 
    if(count<=0){ 
    dba.InsertCurrentCoord(id,id); 
    }else{ 
    Strsql = new String("UPDATE " + ConstantClass.dbName[1] + " SET " + DataBaseAdapter.Key_ReceiverCoord + " = " + 
    Integer.toString(id) + " WHERE " + DataBaseAdapter.Key_ID + " = ?"); 
    dba.UpdateQuery(Strsql, Integer.toString(id)); 
    } 
    dba.Close(); 
    ////////////sending to SMSSync class////////////// 
    MainThread th = new MainThread(sync,msg); 
    try{ 
    th.thread.join(); 
    }catch(Exception e){ 
    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show(); 
    } 
    if(msg.substring(3, 4).equals("2")) 
    ConstantClass.isAuditrequestSend = true; 
    } 
    /*******after receiving the sms opening the Main Screen.*****************/ 
    Intent intent = new Intent(context,ZigbeeActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(intent); 
    abortBroadcast(); 
    /************Now deleting the SMS from the Inbox*********************/ 
    Uri uriSms = Uri.parse("content://sms"); 
    Cursor c = context.getContentResolver().query(uriSms, null,null,null,null); 
    int trId= c.getInt(0); 
    int thread_id =c.getInt(1); 
    context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null); 
    }catch(Exception e){ 
    dlg = new ExceptionDialog(context,"On Sms Receiver",e.getMessage()); 
    dlg.show(); 
    } 
    } 
    }.Set(context).start(); 
    } 
    } 
+0

粘貼代碼以及壓痕。我試圖格式化它,但不是縮進。 – 2012-04-12 07:26:40

+0

爲什麼你第一次保存短信,然後刪除你可以直接限制之前保存在你的onreciev – vipin 2012-04-12 07:31:40

回答

0

試試這個方法,讓我知道發生了什麼,只是通過從消息哪兒來的背景和數量,

注: -

這是用來刪除完整的主題特定數量

private void removeMessage(Context context, String fromAddress) { 

     Uri uriSMS = Uri.parse("content://sms/inbox"); 
     Cursor cursor = context.getContentResolver() 
             .query(uriSMS, null, null, null, null); 
     cursor.moveToFirst(); 
     if(cursor.getCount() > 0){ 
      int ThreadId = cursor.getInt(1); 
      Log.d("Thread Id", ThreadId+" id - "+cursor.getInt(0)); 
      Log.d("contact number", cursor.getString(2)); 
      Log.d("column name", cursor.getColumnName(2)); 

      context.getContentResolver().delete(Uri. 
        parse("content://sms/conversations/"+ThreadId), "address=?", 
                new String[]{fromAddress}); 
      Log.d("Message Thread Deleted", fromAddress); 
     } 
     cursor.close(); 
    } 

此外,像線程的延遲之後調用此方法,

new Thread(new Runnable() { 
    @Override 
    public void run() { 
     try { 
      Thread.sleep(2000); 
      removeMessage(mContext, "From_number"); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
      } 
0

有可能通過您正試圖在收件箱中達到之前或在數據庫表中插入之前刪除短信觀看content://sms所以註冊ContetObserver,當短信收件箱中到達然後刪除it.see例如here