android marshmallow - SMS_RECEIVED permission

前端 未结 5 1369
粉色の甜心
粉色の甜心 2020-12-20 01:51

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

and add

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-20 02:31

    You need to add the permission into manifest xml:

    
    

    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.

提交回复
热议问题