2012-10-05 45 views
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) 

有人可以看看这个,并告诉问题是什么?

+0

哪个版本的Android使用你的代码? –

回答

1

我想你实际上需要的是:

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

它看起来像delete()的使用准备好的语句,这意味着,因为它们是在第三过去了,你就不会通过IDS在声明中字符串论据。

+0

此外,URI需要为“content:// sms”。 – harshit

0

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

注意:不要忘记检查权限读,写短信。和版本> KitKat该应用程序必须是短信的默认应用程序。