1

我想一键删除我的消息中的所有消息。但我已经尝试过下面的编码,它不起作用......有人可以帮我实现这个目标吗?谢谢...

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

我应该在清单中添加什么?使用我的 Galaxy Tab 2 进行测试时强制关闭

4

4 回答 4

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();
    }
于 2012-11-08T06:03:24.160 回答
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;

您还可以参考此处此处以获取有关此主题的更多详细信息。希望它会帮助你。

于 2012-11-08T05:57:13.177 回答
0

要从收件箱中删除短信,您需要使用清单中声明的​​权限

 <uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>

现在你将能够删除你的收件箱短信,现在你不会强制关闭你的模拟器

于 2013-02-06T12:27:36.023 回答
0

今天我找到了不循环删除所有消息的方法,只需通过查询即可。

mContext.getContentResolver().delete(Uri.parse("content://sms/"), null, null);

点击此链接了解更多详情 http://www.wisdomitsol.com/blog/android/sms/programmatically-delete-all-messages-in-android

于 2014-01-16T12:10:16.683 回答