加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 以编程方式删除短信不起作用

发布时间:2020-12-15 01:03:27 所属栏目:Java 来源:网络整理
导读:我想删除我的模拟器中的短信. 但在尝试了所有这些例子之后,它仍然无效. 我甚至没有得到错误. 在我的活动中,我创建了一个BroadcastReceiver,它对传入的SMS作出反应. 然后,当处理完成时,应删除SMS. 但即使我尝试删除所有邮件,我也无法删除任何邮件. 但我可以阅

我想删除我的模拟器中的短信.
但在尝试了所有这些例子之后,它仍然无效.

我甚至没有得到错误.
在我的活动中,我创建了一个BroadcastReceiver,它对传入的SMS作出反应.
然后,当处理完成时,应删除SMS.
但即使我尝试删除所有邮件,我也无法删除任何邮件.
但我可以阅读内容.

也许有人有想法?到目前为止,这是我的代码:

@Override
public void onResume() {
    super.onResume();
    //registerReceiver(mMessageReceiver,new IntentFilter(Constants.BROADCAST_SMS));
    //  IntentFilter customIntentFiler = new     IntentFilter("android.provider.Telephony.SMS_RECEIVED");
    //customIntentFiler.setPriority(1000);
    registerReceiver(mMessageReceiver,new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
    // registerReceiver(mMessageReceiver,customIntentFiler);
}
@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mMessageReceiver);
}

//This is the handler that will manage to process the broadcast intent
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context,Intent intent) {
        // Extract data included in the Intent

        Bundle bundle = intent.getExtras();
        if (bundle != null) {

            Log.d("[" + Constants.SERVICE_SMS + "] " + "SMS received");

            Object[] pdus = (Object[])bundle.get("pdus");
            SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[0]);



            //check if SMS content 

            Log.d("[" + Constants.SERVICE_SMS + "] " + "SMS data:" +     sms.getMessageBody().toString());
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

            String[] smsParts = sms.getMessageBody().toString().split(";");


            //ToDo filter logic
        }
    }
}; 

private void DeleteSMS(Context context,Intent intent){
  try {
 // mLogger.logInfo("Deleting SMS from inbox");
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(uriSms,new String[] { "_id","thread_id","address","person","date","body" },null,null); 

      Log.d(Constants.TAG,"[" + Constants.SERVICE_SMS + "] " + "Deleting SMS" + uriSms);

      //getContentResolver().delete(Uri.parse("content://sms/1"),null);

if (c != null && c.moveToFirst()) {
    do {
        long id = c.getLong(0);
        long threadId = c.getLong(1);
        String address = c.getString(2);
        String body = c.getString(5);
        Log.d(Constants.TAG,"[" + Constants.SERVICE_SMS + "] " + "Deleting SMS ID"+ id);
        Log.d(Constants.TAG,"[" + Constants.SERVICE_SMS + "] " + "Deleting SMS threadID" + threadId);
        Log.d(Constants.TAG,"[" + Constants.SERVICE_SMS + "] " + "Deleting SMS address" + address);
        Log.d(Constants.TAG,"[" + Constants.SERVICE_SMS + "] " + "Deleting SMS body" + body);
        getContentResolver().delete(Uri.parse("content://sms/inbox/" + id),null);
        //getContentResolver().delete(Uri.parse("content://sms/inbox/" + threadId),null);
    } while (c.moveToNext());
        }
    } catch (Exception e) {
        // mLogger.logError("Could not delete SMS from inbox: " +
        // e.getMessage());
      Log.d(Constants.TAG,"[" + Constants.SERVICE_SMS + "] " + "Deleting SMS failed");
    }
}

我的Manifest的开头看起来像这样:

最佳答案
如果您在kitkat之后尝试此应用程序,它将无法正常工作.

http://android-developers.blogspot.com.tr/2013/10/getting-your-sms-apps-ready-for-kitkat.html?m=1

在游戏商店看到sms拦截器,他们会告诉你同样的事情.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读