Android marshmallow request permission?

后端 未结 24 2121
無奈伤痛
無奈伤痛 2020-11-21 06:40

I am currently working on an application that requires several \"dangerous\" permissions. So I tried adding \"ask for permission\" as required in Android Marshmallow(API Lev

24条回答
  •  青春惊慌失措
    2020-11-21 07:06

    To handle runtime permission google has provided a library project. You can check this from here https://github.com/googlesamples/easypermissions

    EasyPermissions is installed by adding the following dependency to your build.gradle file:

    dependencies {
    compile 'pub.devrel:easypermissions:0.3.0'
    }
    

    To begin using EasyPermissions, have your Activity (or Fragment) override the onRequestPermissionsResult method:

    public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
    
    @Override
    public void onPermissionsGranted(int requestCode, List list) {
        // Some permissions have been granted
        // ...
    }
    
    @Override
    public void onPermissionsDenied(int requestCode, List list) {
        // Some permissions have been denied
        // ...
    }
    }
    

    Here you will get a working example how this library works https://github.com/milon87/EasyPermission

提交回复
热议问题