2

i recently updated my app to support android 6 marshmallow. i followed the instruction on https://developer.android.com/training/permissions/requesting.html

and added requestPermissions for Manifest.permission.RECEIVE_SMS. when im runing the following code :

        Log.i(TAG, "sending SMS...");
        Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVED");
        intent.putExtra("pdus", data);

        getContext().sendOrderedBroadcast(intent, null);

i get

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.provider.Telephony.SMS_RECEIVED from pid=1999, uid=10056

i cant send sms broadcast on the device even if i grant SMS_RECEIVED permission.

any idea why i get this security exception on android 6.

my goal is to generate a fake sms in my device link[can I send "SMS received intent"? . i didnt find any mentions on google that its not permitted anymore .

Community
  • 1
  • 1
Jonny
  • 196
  • 1
  • 1
  • 11

5 Answers5

11

You need to add the permission into manifest xml:

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

AND, you need to ask for the permission at runtime. Until android 6, the permissions were granted automatically on installation. In android 6 and above, you can install application and not grant the permission. You can use this function in your activity class:

private void requestSmsPermission() {
    String permission = Manifest.permission.RECEIVE_SMS;
    int grant = ContextCompat.checkSelfPermission(this, permission);
    if ( grant != PackageManager.PERMISSION_GRANTED) {
        String[] permission_list = new String[1];
        permission_list[0] = permission;
        ActivityCompat.requestPermissions(this, permission_list, 1);
    }
}

this - your activity.

drzymala
  • 2,009
  • 20
  • 26
  • The OP isn't really trying to receive messages here. They're trying to send an `SMS_RECEIVED` broadcast to fake incoming messages. The `RECEIVE_SMS` permission will not help here. – Mike M. Nov 12 '16 at 03:56
2

The Android 6 runtime permission android.provider.Telephony.SMS_RECEIVED gives you permission to receive that message when it is sent by the system SMS provider.

You however are trying to broadcast that message yourself. I'm not sure that is permitted, and as you have found is not controlled by the same permission. (In fact, I assume that it has been locked down on Marshmallow so that only the system is able to notify apps of received SMS messages).

zmarties
  • 4,809
  • 22
  • 39
  • my goal is to generate a fake sms in my device http://stackoverflow.com/questions/12489716/can-i-send-sms-received-intent . i didnt find any mentions on google that its not permitted anymore . do you have another sugest how i can generate a fake sms in my app ? – Jonny Oct 27 '15 at 07:03
1

You need a permission for api level 23+, google reworked the permission system so the app user can grant and revoke permissions after installing your app

final private int REQUEST_CODE_ASK_PERMISSIONS = 123;       

  if(Build.VERSION.SDK_INT < 23){
     //your code here
   }else {
    requestContactPermission();
   } 

private void requestContactPermission() {

   int hasContactPermission =ActivityCompat.checkSelfPermission(context,Manifest.permission.RECEIVE_SMS);

   if(hasContactPermission != PackageManager.PERMISSION_GRANTED ) {
    ActivityCompat.requestPermissions(Context, new String[]   {Manifest.permission.RECEIVE_SMS}, PERMISSION_REQUEST_CODE);
   }else {
    //Toast.makeText(AddContactsActivity.this, "Contact Permission is already granted", Toast.LENGTH_LONG).show();
     }
}


@Override
public void onRequestPermissionsResult(int requestCode, String[]           permissions, int[] grantResults) {
switch (requestCode) {
    case REQUEST_CODE_ASK_PERMISSIONS:
        // Check if the only required permission has been granted
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.i("Permission", "Contact permission has now been granted. Showing result.");
            Toast.makeText(this,"Contact Permission is Granted",Toast.LENGTH_SHORT).show();
        } else {
            Log.i("Permission", "Contact permission was NOT granted.");
         } 
         break;
  }
}
O_o
  • 1,103
  • 11
  • 36
0

Android 6.0 / SDK 23 introduces a new way of requesting permissions.

You need to request the SMS permission, see the link below for how to handle permissions:

https://developer.android.com/training/permissions/index.html

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56
  • as i said i follow the instruction and added SMS_RECEIVED permission. i get "not allowed to send broadcast android.provider.Telephony.SMS_RECEIVED " – Jonny Oct 26 '15 at 14:41
  • added or requested? two different things. Adding the permission to manifest will not work – AndroidEnthusiast Oct 26 '15 at 14:43
  • A regular SDK app cannot hold the permission necessary to send an `SMS_RECEIVED` broadcast. Any SMS permission that it could successfully request will not help here. – Mike M. Nov 12 '16 at 04:01
  • My old `targetSdkVersion` was `23` then updated to `27` previously I have given all permissions after app update with new version `oppo F5` asking for `write to sms logs` permission how to check for this granted or not ? – Sagar Sep 27 '18 at 09:57
0

You must grant SMS permission to your app after installation. Just go to Settings > Apps > Your_app > Permissions and then grant the required permission.

Rohit Rokde
  • 622
  • 2
  • 8
  • 16
  • A regular SDK app cannot hold the permission necessary to send an `SMS_RECEIVED` broadcast. Enabling SMS permissions in the app's settings will not help. – Mike M. Nov 12 '16 at 04:00