gpt4 book ai didi

android - 无法删除短信

转载 作者:行者123 更新时间:2023-11-29 02:00:50 25 4
gpt4 key购买 nike

我正在尝试删除短信。

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)

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

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

有人能看一下这个并说出问题是什么吗?

最佳答案

我认为你真正需要的是:

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

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

关于android - 无法删除短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com