gpt4 book ai didi

android - 从另一个 Activity Android 中删除短信

转载 作者:搜寻专家 更新时间:2023-11-01 09:31:54 25 4
gpt4 key购买 nike

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

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

最佳答案

这里是如何删除短信的指南 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());
}

关于android - 从另一个 Activity Android 中删除短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46482005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com