短信:专栏" _id"不存在

时间:2014-05-10 13:54:36

标签: android sms android-4.4-kitkat

尝试使用此代码仅删除SMS中的一条(最后一条)消息:

 Uri uriSMSURI = Uri.parse("content://sms/conversations/");
    Cursor cur = mContext.getContentResolver().query(uriSMSURI, null, null, null, null);
    if (cur.moveToFirst()) {
        String pid0 = cur.getString(1);

 /*Force Close Here, illegal State for the Cursor, Couldn't Read Row 0, Col -1, From CursorWindows : 

 String pid = cur.getString(cur.getColumnIndex("_id"));*/


        String where = "thread_id=" + pid;
        String[] Columns= cur.getColumnNames();
        // do some process
        Log.e("TAG", "content://sms/conversations/" + pid0 + "/" + pid);


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

方法getColumnNames()仅返回3列:“thread_id”,“msg_count”,“snippet”,我正在寻找分配给每条消息的“_id”,我在尝试阅读时会收到强制关闭“_id “,怎么了?

编辑:我现在能够获取消息ID,但无法删除消息,没有发生任何事情:

String id0 = cur.getString(0);
mContext.getContentResolver().delete(Uri.parse("content://sms/" + id0), null, null);

什么是错的?

由于

1 个答案:

答案 0 :(得分:0)

解决了,问题是我在查询"内容://短信/对话/"而不是" content:// sms /",

代码:

Uri uriSMSURI = Uri.parse("content://sms/");
Cursor cur = mContext.getContentResolver().query(uriSMSURI, null, null, null, null);
if (cur.moveToFirst()) {
    ////Changed to 0 to get Message id instead of Thread id : 
    String MsgId= cur.getString(0);



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

由于