Android 无法删除短信

Android 无法删除短信,android,sms,Android,Sms,我正在尝试删除短信 String URI = "content://sms/inbox"; Cursor cursor = getContentResolver().query(Uri.parse(URI), null,null, null, null); String[] _id = new String[cursor.getCount()]; String[] thread_id = new String[cursor.getCount()]; if (cursor.moveToFirst(

我正在尝试删除短信

String URI = "content://sms/inbox";
Cursor cursor = getContentResolver().query(Uri.parse(URI), null,null, null, null);
String[] _id = new String[cursor.getCount()];
String[] thread_id = new String[cursor.getCount()];
if (cursor.moveToFirst()) {
    for (int i = 0; i < cursor.getCount(); i++) {
        try {
            _id[i] = cursor.getString(cursor.getColumnIndexOrThrow("_id")).toString();
            thread_id[i] = cursor.getString(cursor.getColumnIndexOrThrow("thread_id")).toString();
        } catch (IllegalArgumentException e) {
            Log.d(TAG, e.toString());
        }
    }
}
final String id = _id[0]; // for debugging deleting the first msg
final String tid = thread_id[0]; // for debugging deleting the first msg
((Button) findViewById(R.id.delete)).setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
            Log.d(TAG, "id="+id+",tid="+tid);
            getContentResolver().delete(Uri.parse(URI),"_id=" + id + " and thread_id=" + tid,new String[] { String.valueOf(id),String.valueOf(tid) });
            Toast.makeText(getApplicationContext(), "SMS DELETED. RESTARTING ACTIVITY", Toast.LENGTH_LONG);
            Intent i = getApplicationContext().getPackageManager().getLaunchIntentForPackage(getApplicationContext().getPackageName());
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
    }
});
删除删除时引发异常。我的舱单中有以下内容:

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

有人能看一下这个问题并告诉你问题出在哪里吗?

我想你真正需要的是:

getContentResolver().delete(Uri.parse(URI),"_id=? and thread_id=?",new String[] { String.valueOf(id),String.valueOf(tid) });

delete()似乎使用了准备好的语句,这意味着您不会在语句字符串中传递ID,因为它们是在第三个参数中传递的。

MyActivity.java

    private static final String ADDRESS_COLUMN_NAME = "address";
    private static final String DATE_COLUMN_NAME = "date";
    private static final String BODY_COLUMN_NAME = "body";
    private static final String ID_COLUMN_NAME = "_id";

    (...)

    // Defines selection criteria for the rows you want to delete
    String mSelectionClause = ADDRESS_COLUMN_NAME + " = ? AND " + BODY_COLUMN_NAME + " = ? AND " + DATE_COLUMN_NAME + " = ?";

    String[] mSelectionArgs = new String[3];
    mSelectionArgs[0] = currentSms.getAddress();
    mSelectionArgs[1] = currentSms.getMsg();
    mSelectionArgs[2] = currentSms.getDate().toString();

    ContentResolver contentResolver = getContentResolver();

    // Delete from database
    int rowsDeleted = 0;
    rowsDeleted = contentResolver.delete(
            Uri.parse("content://sms"),   // the sms content URI
            mSelectionClause,                    // the column to select on
            mSelectionArgs                       // the value to compare to
    );

    if (rowsDeleted > 0) {
        Log.d(TAG, "Success!");
    } else {
        Log.d(TAG, "Delete sms UNSUCCESSFULL!!!");
    }

注意:别忘了检查读写短信的权限。和versions>KitKat应用程序必须是短信的默认应用程序。

此外,URI必须是
content://sms
。哪个版本的Android使用了您的代码?
getContentResolver().delete(Uri.parse(URI),"_id=? and thread_id=?",new String[] { String.valueOf(id),String.valueOf(tid) });
    private static final String ADDRESS_COLUMN_NAME = "address";
    private static final String DATE_COLUMN_NAME = "date";
    private static final String BODY_COLUMN_NAME = "body";
    private static final String ID_COLUMN_NAME = "_id";

    (...)

    // Defines selection criteria for the rows you want to delete
    String mSelectionClause = ADDRESS_COLUMN_NAME + " = ? AND " + BODY_COLUMN_NAME + " = ? AND " + DATE_COLUMN_NAME + " = ?";

    String[] mSelectionArgs = new String[3];
    mSelectionArgs[0] = currentSms.getAddress();
    mSelectionArgs[1] = currentSms.getMsg();
    mSelectionArgs[2] = currentSms.getDate().toString();

    ContentResolver contentResolver = getContentResolver();

    // Delete from database
    int rowsDeleted = 0;
    rowsDeleted = contentResolver.delete(
            Uri.parse("content://sms"),   // the sms content URI
            mSelectionClause,                    // the column to select on
            mSelectionArgs                       // the value to compare to
    );

    if (rowsDeleted > 0) {
        Log.d(TAG, "Success!");
    } else {
        Log.d(TAG, "Delete sms UNSUCCESSFULL!!!");
    }