我想删除消息中的所有消息,只需单击按钮即可。但我已经尝试过下面的编码,它不起作用......有人可以帮助我实现这个目标吗?感谢...
public class DeleteSMSActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button press;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
press = (Button)findViewById(R.id.button1);
press.setOnClickListener(this);
}
public void onClick(View view){
ContentResolver cr = getContentResolver();
Uri inboxUri = Uri.parse("content://sms/inbox");
Cursor c = cr.query(inboxUri , null, null, null, null);
while (c.moveToNext()) {
// Delete the SMS
String pid = c.getString(0); // Get id;
String uri = "content://sms/" + pid;
cr.delete(Uri.parse(uri), null, null);
}
}
}
我应该在Manifest中添加什么?使用我的Galaxy Tab 2进行测试时关闭力量
答案 0 :(得分:7)
尝试使用_id
删除
Cursor c = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/"), null, null, null,null);
try {
while (c.moveToNext()) {
int id = c.getInt(0);
getApplicationContext().getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
}
}catch(Exception e){
Log.e(this.toString(),"Error deleting sms",e);
}finally {
c.close();
}
答案 1 :(得分:0)
删除uri是“content:// sms /”+ id;
Uri inboxUri = Uri.parse("content://sms/inbox");
int count = 0;
Cursor c = context.getContentResolver().query(inboxUri , null, null, null, null);
while (c.moveToNext()) {
try {
// Delete the SMS
String pid = c.getString(0); // Get id;
String uri = "content://sms/" + pid;
count = context.getContentResolver().delete(Uri.parse(uri),
null, null);
} catch (Exception e) {
}
}
return count;
答案 2 :(得分:0)
要从收件箱中删除短信,您需要使用清单中声明的权限
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
现在你可以删除你的收件箱短信,现在你不会让你的力量关闭你的模拟器
答案 3 :(得分:0)
今天我找到了无需循环删除所有邮件的方法,只需查询。
mContext.getContentResolver().delete(Uri.parse("content://sms/"), null, null);
请点击此链接了解更多详情 http://www.wisdomitsol.com/blog/android/sms/programmatically-delete-all-messages-in-android