-1

I am getting the error:

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

Please help me to solve this as I'm new for this.

My code is:

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>

I added all permissions, but I am still not getting an answer and the same error is displayed...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Namrata
  • 137
  • 1
  • 2
  • 12
  • 2
    on which API level you are running your code – Akshay Nov 22 '16 at 06:00
  • For API 6 and above you need user permission for external storage reading/writing, Sending/ Receiving SMS, accessing camera etc at runtime. follow link (http://www.theappguruz.com/blog/runtime-permissions-in-android-marshmallow) will help you. – Akshay Nov 22 '16 at 06:08
  • For Android 4.4 only default app can delete SMS, if User made you App as default it can delete SMS as well. Refer link (http://stackoverflow.com/questions/8614211/deleting-android-sms-programatically) for more details. – Akshay Nov 22 '16 at 06:14
  • I used API level 4.2.2 jellybean – Namrata Nov 22 '16 at 06:19
  • @Zilu How many times you will ask the [same question](http://stackoverflow.com/q/40903886/1761003) ??? – Maveňツ Dec 01 '16 at 12:48

3 Answers3

2

As you are saying you are running you code on 4.2.2, not need to request permission for it. But for deleting an SMS you must add these permissions in AndroidManifest.xml:

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

you missed the READ_SMS permission, before delete operation you have to perform read operation which will need it.

Akshay
  • 6,029
  • 7
  • 40
  • 59
0

Have you given permission to read SMSes in the manifest? If you test on Android 6.0 and above, you must provide permission at runtime.

Example to read contacts:

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);
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SaravInfern
  • 3,338
  • 1
  • 20
  • 44
0

In Android 6.0 (Marshmallow) and above you have to take the permission at runtime.

 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)
                {
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sagar Gangawane
  • 1,985
  • 1
  • 12
  • 22