2016-07-26 57 views
2

我嘗試從我的手機刪除所有短信與此FUNC:無法刪除短信編程(默認短信應用集)

public void wipeOutAllSMS(Context context) { 
    try { 

     int rowsDeleted = 0; 

     Uri uriSms = Uri.parse("content://sms/inbox"); 
     Cursor c = context.getContentResolver().query(uriSms, new String[]{"_id", "thread_id", "address", "person", "date", "body"}, "read=0", null, null); 

     if (c != null && c.moveToFirst()) { 
      do { 
       long id = c.getLong(0); 
       long threadId = c.getLong(1); 
       String address = c.getString(2); 
       String body = c.getString(5); 
       String date = c.getString(3); 

       // mLogger.logInfo("Deleting SMS with id: " + threadId); 
       rowsDeleted += context.getContentResolver().delete(Uri.parse("content://sms/" + id), "date=?", new String[]{c.getString(4)}); 

       Log.i("Deleting_SMS", "Delete success for: " + address + ", message = " + body); 

      } while (c.moveToNext()); 
     } 

     Log.i("Deleting_SMS", "rows deleted: " + rowsDeleted); 

    } catch (Exception e) { 
     Log.e("Deleting_SMS", e.toString()); 
    } 
} 

不幸的是,「rows_deleted」始終爲0它不會刪除一個短信。

我已經加入我的清單中的以下內容:

權限:

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

其他的事情,爲了能夠選擇我的應用程序爲默認的短信讀者:

<!--The following things are needed for this app to became the default sms app in order to have the ability to delete an sms. --> 

     <!-- BroadcastReceiver that listens for incoming SMS messages --> 
     <receiver 
      android:name=".android.broadcast.SmsReceiver" 
      android:permission="android.permission.BROADCAST_SMS"> 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_DELIVER" /> 
      </intent-filter> 
     </receiver> 


     <!-- BroadcastReceiver that listens for incoming MMS messages --> 
     <receiver 
      android:name=".android.broadcast.MmsReceiver" 
      android:permission="android.permission.BROADCAST_WAP_PUSH"> 
      <intent-filter> 
       <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" /> 
       <data android:mimeType="application/vnd.wap.mms-message" /> 
      </intent-filter> 
     </receiver> 

     <!-- This one needs too--> 


     <!-- Service that delivers messages from the phone "quick response" --> 
     <service android:name=".android.broadcast.HeadlessSmsSendService" 
      android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE" 
      android:exported="true" > 
      <intent-filter> 
       <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:scheme="sms" /> 
       <data android:scheme="smsto" /> 
       <data android:scheme="mms" /> 
       <data android:scheme="mmsto" /> 
      </intent-filter> 
     </service> 

我還要求對應用程序啓動的權限,我最終接受:

if (!PermissionHelper.isPermissionGranted(this, Manifest.permission.SEND_SMS) || !PermissionHelper.isPermissionGranted(this, Manifest.permission.READ_SMS)) { 
      PermissionHelper.requestPermission(this, 
        new String[]{ 
        Manifest.permission.SEND_SMS, 
        Manifest.permission.READ_SMS 
      }); 
} 

WRITE_SMS權限不Manifest.permisson存在,所以我不能要求這個。)

我也有設置我的設備上的默認短信讀者到我的應用程序。但是我無法刪除單個短信。

如果可以,請幫助。

我用android 棉花糖,沒有拋出異常。

+0

您是否看到過logcat中的任何消息 –

回答

1

如果你是默認的應用程序,那麼你可以像這樣刪除。

Uri.Builder builder; 
    builder = Sms.CONTENT_URI.buildUpon(); 
    //builder.appendEncodedPath(getSms_id()); 
    builder.appendEncodedPath("your SMS ID here"); 
    Uri uri = builder.build(); 
    context.getContentResolver().delete(uri, null, null);