【问题标题】:Delete sms from another activity Android从另一个活动 Android 中删除短信
【发布时间】:2017-09-29 14:32:01
【问题描述】:

我在自定义列表视图中使用 ("content://sms/inbox") 访问所有短信,目前我正在获取地址正文和 _id 现在我想从另一个活动中删除选定的短信请指导我,我是 andorid 的初学者 这是我的主要活动,但我想从另一个活动中删除选定的短信

 Uri uri = Uri.parse("content://sms/");
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(uri, null, null, null, null, null);

    if(cursor !=null  && cursor.moveToFirst()){
        do{
        //    name = getContactName(address);
             tid=             cursor.getString(cursor.getColumnIndexOrThrow("_id"));
            address = cursor.getString(cursor.getColumnIndexOrThrow("address"));
             body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
          if(name==null) {

                list.add(new mybean("" + address, "" + body,""+tid));

            }

            else{
                list.add(new mybean("" + name, "" + body,""+tid));
            }
            my =new  myadapter(this,list);
            lv.setAdapter(my);
        }while(cursor.moveToNext());

    }



        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                Intent intent =new Intent(MainActivity.this,Main2Activity.class);

                intent.putExtra("delete",list.get(pos).getDel());
                intent.putExtra("sms",list.get(pos).getNumber());
                intent.putExtra("smsmsg",list.get(pos).getMsg());


                startActivity(intent);
            }
        });

【问题讨论】:

  • 自 KitKat (4.4) 以来,您的应用无法从 Provider 中删除 SMS,除非它是当前的默认消息应用。
  • 是的,我现在理解逻辑谢谢

标签: android


【解决方案1】:

这里是如何删除短信的教程 Deleting Android SMS programmatically 对于奇巧 https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html 首先,您应该选择您的应用程序作为默认短信应用程序,然后您可以从那里删除或删除短信.. 你也可以参考这个帖子 How to delete an SMS from the inbox in Android programmatically? 这是以编程方式删除短信的教程 http://wisdomitsol.com/blog/android/sms/programmatically-delete-sms-in-android 我希望这些帖子对您有所帮助,如果有任何问题可以在这里发表评论。

1.首先在manifest中添加权限 2.写方法

public boolean deleteSms(String smsId) {
    boolean isSmsDeleted = false;
    try {
        mActivity.getContentResolver().delete(
                Uri.parse("content://sms/" + smsId), null, null);
        isSmsDeleted = true;

    } catch (Exception ex) {
        isSmsDeleted = false;
    }
    return isSmsDeleted;
}

您现在可以按 ID 删除短信

你也可以试试这个代码

try {
    Uri uriSms = Uri.parse("content://sms/inbox");
    Cursor c = context.getContentResolver().query(
            uriSms,
            new String[] { "_id", "thread_id", "address", "person",
                    "date", "body" }, "read=0", null, 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);
            String date = c.getString(3);
            Log.e("log>>>",
                    "0--->" + c.getString(0) + "1---->" + c.getString(1)
                            + "2---->" + c.getString(2) + "3--->"
                            + c.getString(3) + "4----->" + c.getString(4)
                            + "5---->" + c.getString(5));
            Log.e("log>>>", "date" + c.getString(0));

            ContentValues values = new ContentValues();
            values.put("read", true);
            getContentResolver().update(Uri.parse("content://sms/"),
                    values, "_id=" + id, null);

            if (message.equals(body) && address.equals(number)) {
                // mLogger.logInfo("Deleting SMS with id: " + threadId);
                context.getContentResolver().delete(
                        Uri.parse("content://sms/" + id), "date=?",
                        new String[] { c.getString(4) });
                Log.e("log>>>", "Delete success.........");
            }
        } while (c.moveToNext());
    }
} catch (Exception e) {
    Log.e("log>>>", e.toString());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    相关资源
    最近更新 更多