0

请有人告诉我如何删除在 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();
    }
    }
4

2 回答 2

0

可能您正在尝试在到达收件箱之前或在插入数据库表之前删除一条短信,因此请注册一个 ContetObserver 用于观看的content://sms短信,当短信到达收件箱时将其删除。请参见此处的示例

于 2012-04-12T07:37:41.630 回答
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();
    }

此外,在 Thread 延迟后调用此方法,例如,

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            removeMessage(mContext, "From_number");
        } catch (InterruptedException e) {
            e.printStackTrace();
             }
于 2012-04-12T07:43:28.810 回答