2016-11-22 56 views
-1

我收到錯誤:如何從應用程序中刪除了手機收件箱中短信在Android

Permission Denial:writing`com.android.provider.telephony.smsprovider uri content://sms/472 from pid=11211, uid=10142 requires 
android.permission.write_sms, or granturipermission() 

請幫我解決這個問題,因爲我是新本。

我的代碼是:

public void deleteSMS() 
{ 
    Uri inboxUri = Uri.parse("content://sms/inbox"); 
    int count = 0; 
    Cursor c = getContentResolver().query(inboxUri , null, null, null, null); 
    while (c.moveToNext()) { 
     try { 
      // Delete the SMS 
      String pid = c.getString(0); // Get id; 
      String uri = "content://sms/" + pid; 
      count =getContentResolver().delete(Uri.parse(uri), 
        null, null); 
      Toast.makeText(getApplicationContext(),"message deleted successfully..!"+count, 5000).show(); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), e.toString(), 5000).show(); 
     } 
    }} 

Added Permission in manifest file: 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.SEND_SMS" /> 
<uses-permission android:name="android.permission.WRITE_SMS"/> 
<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE"/> 
<receiver android:name=".receiver.SMSReceiver" android:enabled="true"> 
    <intent-filter android:priority="1000"> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
</receiver> 

我添加的所有權限,但我仍然沒有得到顯示答案,同樣的錯誤......

+2

您在哪個API級別上運行代碼 – Akshay

+0

對於API 6及更高版本,您需要用戶在外部存儲器讀/寫,發送/接收SMS,訪問攝像機等運行時的權限。按照鏈接(http://www.theappguruz.com/blog/runtime-permissions-in-android-marshmallow)將幫助你。 – Akshay

+0

對於Android 4.4,只有默認的應用程序可以刪除短信,如果用戶使您的應用程序爲默認,它也可以刪除短信。請參閱鏈接(http://stackoverflow.com/questions/8614211/deleting-android-sms-programatically)以獲取更多詳細信息。 – Akshay

回答

2

當你說你是在4.2.2運行你的代碼,無需申請許可的。但對於刪除SMS必須在AndroidManifest.xml添加這些權限:

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

你錯過了READ_SMS許可之前,刪除操作,您必須執行讀操作,這將需要它。

0

你是否獲准來讀取的短信表現?如果您在Android 6.0及更高版本上測試,則必須在運行時提供權限。

實施例來讀取接觸:

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != 
    PackageManager.PERMISSION_GRANTED) { 

    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
     Manifest.permission.READ_CONTACTS)) { 


    } 
    else { 
     ActivityCompat.requestPermissions(
      thisActivity, 
      new String[]{Manifest.permission.READ_CONTACTS}, 
      MY_PERMISSIONS_REQUEST_READ_CONTACTS); 
    } 
} 
+0

爲什麼沒有shouldShowRequestPermissionRationale()的真代碼? –

+0

@ saravInfern我在4.2.2上測試過 – Zilu

0

在Android中6.0(棉花糖)和上述你必須採取允許在運行時。

requestPermissions(new String[]{Manifest.permission.READ_SMS}, 1). 

Check whether you have got the permission or not.

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 

    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

    switch (requestCode) { 

     case 1: 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       if (ActivityCompat.checkSelfPermission(
         getActivity(), Manifest.permission.READ_SMS) == 
        PackageManager.PERMISSION_GRANTED) 
       { 
+0

感謝大家的幫助,我得到了我的答案.. – Zilu

相關問題