您好我尝试在Android上将短信标记为已读。我有以下代码:
public void setRead(int position, String smsMessageId) {
smsBody.get(position).status=true;
ContentValues values = new ContentValues();
values.put("read", true);
context.getContentResolver().update(Uri.parse("content://sms/inbox"),
values, "_id=" + smsMessageId, null);
}
代码的执行发生在这里:
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
try {
SMSItem smsMessageStr = (SMSItem) arrayAdapter.getItem(pos);
if (smsMessageStr.status == false) {
// String smsMessageId = ((SmsArrayAdapter)
// arrayAdapter).getId(pos);
((SmsArrayAdapter) arrayAdapter).setRead(pos, smsMessageStr.ID);
Toast.makeText(this, "ID is " + smsMessageStr.ID, Toast.LENGTH_LONG)
.show();
}
Intent intent = new Intent(SmsActivity.this,
ShowIndividualSMS.class);
intent.putExtra("SMS", smsMessageStr.sms);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "exception is " + e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e("TAG", e.getMessage());
}
}
我已尝试过所有可能的解决方案。为什么我无法将此短信设置为已读?问题出在哪儿?
答案 0 :(得分:2)
试试这个,
Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
// Retrieve sms
// see column "address" for comparing
// Then update the sms and set the column "read" to 1
}
也用,
ContentValues values = new ContentValues();
values.put("read",true);
getContentResolver().update(Uri.parse("content://sms/inbox"),values,
"_id="+SmsMessageId, null);
其中id是消息ID。
答案 1 :(得分:0)
请找到这个
private void markMessageRead(Context context, String number, String body) {
Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
try{
while (cursor.moveToNext()) {
if ((cursor.getString(cursor.getColumnIndex("address")).equals(number)) && (cursor.getInt(cursor.getColumnIndex("read")) == 0)) {
if (cursor.getString(cursor.getColumnIndex("body")).startsWith(body)) {
String SmsMessageId = cursor.getString(cursor.getColumnIndex("_id"));
ContentValues values = new ContentValues();
values.put("read", true);
context.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=" + SmsMessageId, null);
return;
}
}
}
}catch(Exception e)
{
Log.e("Mark Read", "Error in Read: "+e.toString());
}
}