Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android marshmallow - SMS_RECEIVED permission

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 .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...