Detailed Android Rights Management Rxpermission solve the Android 6.0 adaptation problem _android

Source: Internet
Author: User

Objective:

The last article focused on the runtime permissions of the Android 6.0, today or around the Android 6.0 competency to sum up learning, here is a major introduction to our company to solve the Android 6.0 rights matching program: Rxjava+rxpermission. The Android 6.0 runtime permissions are no longer here, and you can see how to use rxpermission directly.

Rxpermission:

Open source framework for adapting to the new Android 6.0 permissions model.

Download Address: Click here to download

How to use it?

1. Add the following configuration to the Build.gradle of app module

Use this open source framework minsdkversion must be less than 9

repositories {
  jcenter ()//If not already there
}

dependencies {
  compile ' com.tbruyelle.rxpermissions : Rxpermissions:0.9.0@aar '
}

If you use the RXJAVA2, use the following configuration

Unlike Rxjava, the package name became COM.TBRUYELLE.RXPERMISSIONS2, but to tell the truth RxJava2 I haven't used it yet.

dependencies {
  compile ' Com.tbruyelle.rxpermissions2:rxpermissions:0.8.2@aar '
}

2.) Direct Application Permission use

Rxpermissions.getinstance (this) is used in the form of a single case, where the permission to apply for a photo is taken as an example

Rxpermissions.getinstance (this)
  . Request (permissions)
  . Subscribe (New action1<boolean> () {
    @ Override public
    void Call (Boolean granted) {
    if (granted) {//will return True by default before Android 6.0
      //have access to permissions
      String J Pgpath = Getcachedir () + "test.jpg";
      Takephotobypath (Jpgpath, 2);
    } else {
      //not get permission
      toast.maketext (Mainactivity.this, "You do not authorize this permission, open authorization in Settings, Toast.length_short). Show ();
    }
  });

Here the direct return whether authorization, we can according to this value carries on the different processing.

3.) Conditional trigger access (combined with rxbinding)

About Rxbinding, you can view this jakewharton/rxbinding

Rxview.clicks (Findviewbyid (r.id.request_permission))
  . Compose (Rxpermissions.getinstance (this). ensure ( Manifest.permission.CAMERA)
  . Subscribe (New action1<boolean> () {
    @Override public
    void Call ( Boolean granted) {

    if (granted) {//Before Android 6.0 returns true
      //has acquired permissions
      String Jpgpath = getcachedir () + "test. JPG ";
      Takephotobypath (Jpgpath, 2);
    } else {
      //not get permission
      toast.maketext (Mainactivity.this, "You do not authorize this permission, open authorization in Settings, Toast.length_short). Show ();
    }
  });

3.) Request multiple permissions at the same time (combined results)

For example, to apply for both camera and recording.

Rxpermissions.getinstance (mainactivity.this). Request (Manifest.permission.CAMERA, Manifest.permission.RECORD_ AUDIO)
  . Subscribe (New action1<boolean> () {
    @Override public
    void Call (Boolean granted) {
    if ( Granted) {//Before Android 6.0 returns true
      //has acquired permissions
      String Jpgpath = getcachedir () + "test.jpg";
      Takephotobypath (Jpgpath, 2);
    } else {
      //not get permission
      toast.maketext (Mainactivity.this, "You do not authorize this permission, open authorization in Settings, Toast.length_short). Show ();
    }
  });

If multiple permissions are requested at the same time, the following method merges the result of the request, that is, the success of all permission requests returns True, or False if one of the permissions is unsuccessful.

4.) Request multiple permissions at the same time (get results separately)

If you want to request multiple permissions at the same time and want to obtain authorization results separately, you can call the Requesteach method or the Ensureeach method

Rxpermissions.getinstance (mainactivity.this). Requesteach (Manifest.permission.CAMERA,
    Manifest.permission.RECORD_AUDIO)
    . Subscribe (New action1<permission> () {
      @Override public
      Void Call (Permission Permission) {
      if (permission.name.equals (Manifest.permission.CAMERA)) {
        if ( permission.granted) {
        String Jpgpath = getcachedir () + "test.jpg";
        Takephotobypath (Jpgpath, 2);
        } else {
        //not get permission
        toast.maketext (Mainactivity.this, "You do not authorize this permission, open authorization in Settings, Toast.length_short). Show ();

      Else if (permission.name.equals (Manifest.permission.RECORD_AUDIO)) {

      }}
      }
    );

Summarize:

Right now we're using rxpermission to do the right thing with Android 6.0.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.