2

我正在尝试删除短信。

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

它抛出一个异常:

10-06 00:48:43.234: E/AndroidRuntime(27912): FATAL EXCEPTION: main
10-06 00:48:43.234: E/AndroidRuntime(27912): java.lang.IllegalArgumentException: Unknown URL
10-06 00:48:43.234: E/AndroidRuntime(27912):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at android.content.ContentProviderProxy.delete(ContentProviderNative.java:483)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at android.content.ContentResolver.delete(ContentResolver.java:675)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at com.example.detector.MainActivity$1.onClick(MainActivity.java:85)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at android.view.View.performClick(View.java:2408)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at android.view.View$PerformClick.run(View.java:8816)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at android.os.Handler.handleCallback(Handler.java:587)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at android.os.Looper.loop(Looper.java:123)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at android.app.ActivityThread.main(ActivityThread.java:4633)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at java.lang.reflect.Method.invokeNative(Native Method)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at java.lang.reflect.Method.invoke(Method.java:521)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-06 00:48:43.234: E/AndroidRuntime(27912):    at dalvik.system.NativeStart.main(Native Method)

删除删除时抛出异常。我的清单中有以下内容:

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

我尝试将 URL 更改content://sms为删除。不工作。给出以下异常:

10-06 01:59:57.602: E/AndroidRuntime(29381): android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x6cfa38
10-06 01:59:57.602: E/AndroidRuntime(29381):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
10-06 01:59:57.602: E/AndroidRuntime(29381):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
10-06 01:59:57.602: E/AndroidRuntime(29381):    at android.content.ContentProviderProxy.delete(ContentProviderNative.java:483)
10-06 01:59:57.602: E/AndroidRuntime(29381):    at android.content.ContentResolver.delete(ContentResolver.java:675)
10-06 01:59:57.602: E/AndroidRuntime(29381):    at com.example.detector.MainActivity$1.onClick(MainActivity.java:85)

有人可以看看这个并说出问题所在吗?

4

2 回答 2

1

我认为你真正需要的是:

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

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

于 2012-10-05T21:44:34.537 回答
0

我的活动.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!!!");
    }

注意:不要忘记检查读、写短信的权限。并且版本 > KitKat 该应用程序必须成为短信的默认应用程序。

于 2016-04-27T10:39:30.977 回答