Skip to content Skip to sidebar Skip to footer

Basic Error In Androidmanifest.xml For Sms Receiving Permission

I know that this has been asked a dozen times before, but I still get the permission error with this xml config. I have scoured the other responses on this. I am using API level 23

Solution 1:

Problem lays in new permissions model for Android M (api 23):

Quickview

  • If your app targets the M Preview SDK, it prompts users to grant permissions at runtime, instead of install time.
  • Users can revoke permissions at any time from the app Settings screen.
  • Your app needs to check that it has the permissions it needs every time it runs.

for SMS case documentation bring an example:

For example, suppose an app lists in its manifest that it needs the SEND_SMS and RECEIVE_SMS permissions, which both belong to android.permission-group.SMS. When the app needs to send a message, it requests the SEND_SMS permission. The system shows the user a dialog box asking if the app can have access to SMS. If the user agrees, the system grants the app the SEND_SMS permission it requested. Later, the app requests RECEIVE_SMS. The system automatically grants this permission, since the user had already approved a permission in the same permission group.

Solutions:

  • right way - request permission at first.
  • lazy way - set targetSdk 22

Solution 2:

First, RECEIVE_SMS permission must be declared in AndroidManifest.xml.

...
<uses-permissionandroid:name="android.permission.RECEIVE_SMS" />
...
<receiverandroid:name=".receiver.IncomingSmsReceiver"android:enabled="true"android:exported="true"><intent-filter><actionandroid:name="android.provider.Telephony.SMS_RECEIVED" /></intent-filter></receiver>

Then, from API level 23, we need to request RECEIVE_SMS permission at run time. This is important to notice. https://developer.android.com/training/permissions/requesting.html

publicclassMainActivityextendsAppCompatActivity {

    privatestaticfinalintPERMISSIONS_REQUEST_RECEIVE_SMS=0;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Request the permission immediately here for the first time run
        requestPermissions(Manifest.permission.RECEIVE_SMS, PERMISSIONS_REQUEST_RECEIVE_SMS);
    }


    privatevoidrequestPermissions(String permission, int requestCode) {
        // Here, thisActivity is the current activityif (ContextCompat.checkSelfPermission(this, permission)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {

                // Show an explanation to the user *asynchronously* -- don't block// this thread waiting for the user's response! After the user// sees the explanation, try again to request the permission.
                Toast.makeText(this, "Granting permission is necessary!", Toast.LENGTH_LONG).show();

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(this,
                        newString[]{permission},
                        requestCode);

                // requestCode is an// app-defined int constant. The callback method gets the// result of the request.
            }
        }
    }

    @OverridepublicvoidonRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSIONS_REQUEST_RECEIVE_SMS: {
                // If request is cancelled, the result arrays are empty.if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the// contacts-related task you need to do.

                    NotificationUtil.getInstance().show(this, NotificationUtil.CONTENT_TYPE.INFO,
                            getResources().getString(R.string.app_name),
                            "Permission granted!");

                } else {

                    // permission denied, boo! Disable the// functionality that depends on this permission.

                    NotificationUtil.getInstance().show(this, NotificationUtil.CONTENT_TYPE.ERROR,
                            getResources().getString(R.string.app_name),
                            "Permission denied! App will not function correctly");
                }
                return;
            }

            // other 'case' lines to check for other// permissions this app might request
        }
    }
}

Hope this help you.

Solution 3:

@Richard Green : Your Logcat throws

Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x8000010 (has extras) } to com.example.richard.simplesmstoast/.SmsReceiver requires android.permission.RECEIVE_SMS due to sender com.android.phone

A permission is a restriction limiting access to a part of the code or to data on the device. The limitation is imposed to protect critical data and code that could be misused to distort or damage the user experience.

I assume that its permissions Problem .

Please add below Manifest -Permissions Above Application Tag .

<uses-permissionandroid:name="INTERNET"/><uses-permissionandroid:name="ACCESS_NETWORK_STATE"/><uses-permissionandroid:name="android.permission.WRITE_SMS" /><uses-permissionandroid:name="android.permission.READ_SMS" /><uses-permissionandroid:name="android.permission.RECEIVE_SMS" />

I hope it will helps you .

Solution 4:

Your permission should be placed outside and before your application tag:

<uses-permissionandroid:name="android.permission.RECEIVE_SMS" />

Solution 5:

  • If you are targeting Marshmallow then you have to request for runtime permission apart from manifest.

Manifest -

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

Java Activity class -

finalintREQ_CODE=100;
voidrequestPermission(){
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
        CTLogs.printLogs( "Permission is not granted, requesting");
        ActivityCompat.requestPermissions(this, newString[]{Manifest.permission.SEND_SMS,Manifest.permission.READ_SMS,Manifest.permission.RECEIVE_SMS}, REQ_CODE);

    } else {
        CTLogs.printLogs("Permission has been granted");
        readSMS();
    }
}

@OverridepublicvoidonRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQ_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            CTLogs.printLogs("Permission has been granted");
            readSMS();
        } else {
            CTLogs.printLogs("Permission denied !!!");
        }
    }
}

Post a Comment for "Basic Error In Androidmanifest.xml For Sms Receiving Permission"