簡體   English   中英

如何刪除在onReceive方法上捕獲的相同短信

[英]How to delete same sms that was captured on onReceive method

請有人告訴我,如何刪除在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();
    }
    }

可能您嘗試在到達收件箱之前或在插入數據庫表之前刪除短信,因此注冊一個ContetObserver來監視content://sms ,當短信到達收件箱后再將其刪除。請參見此處的示例

嘗試這種方法,讓我知道會發生什么,只需傳遞消息所來自的上下文和編號,

注意:-

這用於刪除特定編號的完整線程

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

另外,請在Thread延遲后調用此方法,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM