首先,我对Android编程非常陌生。
我写了一个删除短信的程序。我已经在Jelly Bean中测试了这段代码,它运行正常。即使在使用Android的模拟器中,Marshmallow也能正常工作。最近我将手机更新为Marshmallow,现在短信没有从应用程序中删除。甚至没有错误抛出。下面是代码。我搜索解决方案并尝试了不同的方法,但仍然没有解决方案。
public void delete(final String id, final String number, final long date) {
Uri uri = Uri.parse("content://sms/inbox");
ContentResolver cr = context.getContentResolver();
Cursor c = cr.query(uri, null, "person is null and _id = ?", new String[] { "" + id }, null);
if (c != null && c.moveToFirst()) {
do {
String pid = c.getString(c.getColumnIndex("_id"));
String pnumber = c.getString(c.getColumnIndex("address"));
Log.d("MyAPP", "id: " + pid);
if (id.equals(pid) && number.equals(pnumber)) {
Log.d("MyAPP", "Deleting SMS with id: " + id);
try{
cr.delete(Uri.parse("content://sms/" + id), null, null);
Log.d("MyAPP", "Delete success.........");
} catch (Exception ex) {
Log.d("MyAPP", "Error deleting msg");
}
}
} while (c.moveToNext());
}
}
这是日志。
04-01 22:49:04.783: (1620): id: 39
04-01 22:49:04.783: (1620): Deleting SMS with id: 39
04-01 22:49:04.785: (1620): Delete success.........