Using Rxpermissions (based on RXJAVA2)
0. Background
Android 6.0 (API level 23). The permissions are divided into two categories. A class is the Install permission (called the installation-time permission ). There is also a class of runtime permissions (called Execute-time permissions ).
What is the install permission ?
Install permissions : installation Permissions , as the name implies, is when the app is installed. The permissions granted to the app, that is, the permissions acquired immediately after installation. Both normal and signature levels are permissions at install time. Given the app Normal and signature permissions, the user will not be prompted to the interface, the system itself to decide the authority of the grant . For signature permissions, the system denies granting the signature permission if the app that uses the permission does not have the same signature as the app that claims the permission.
What is runtime permissions ?
Runtime Permissions : Execute-time permissions . Refers to the process in which the app executes. Give the app permission. In this process, the explicit permission-granting interface is displayed, allowing the user to decide whether to grant permissions . Assuming that the app targetSdkVersion is Lollipop MR1 and below, dangerous permissions are installation- time permissions , otherwise dangerous permissions are Execute-time permissions .
Suppose that an app targetSdkVersion is 23 (or more than 23). Then all dangerous permissions that the app applies for are execute-time permissions. Assuming that the app executes in an Android 6.0 environment, it must actively request these dangerous permissions (calls) when it executes requestPermissions() . Otherwise, the app does not get the dangerous permission.
Many other descriptions of permissions, please refer to: "Some details of Android permissions"
1. Advantages of Rxpermissions
Rxpermissions is to help developers simplify requestPermissions() related processing.
boolean isMarshmallow() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; } publicbooleanisGranted(String permission) { // 假设是Android 6.0 (Api 23)之前,则权限被同意使用。 return !isMarshmallow() || mRxPermissionsFragment.isGranted(permission); }
(2) The code () of the permission request is managed together with the code () of the requestPermissions() request result onRequestPermissionsResult() , which avoids the dispersion of the code.
(3) Rxpermissions has rx (RxJava) characteristics, such as the ability to use chain operation, to perform the filter operation, transform operation. Wait a minute.
2. Version number of the Rxpermissions
Rxpermissions is based on Rxjava, Rxjava now has 2 large version numbers. A Rxjava 1.x (usually said Rxjava), and one is RxJava2.
So rxpermissions has 2 version numbers that support Rxjava and RXJAVA2, respectively.
The source code for Rxpermissions is https://github.com/tbruyelle/RxPermissions.
The master branch is the version number that supports Rxjava 1.x, and the package name is com.tbruyelle.rxpermissions . 2.xThe branch is the version number that supports RXJAVA2. The package name is com.tbruyelle.rxpermissions2 .
The default is to see the master branch, so the code you see is the rxpermissions that supports Rxjava 1.x.
Selection of branches (for example):
After clicking on "Branch:master", you can see that there are now 3 branches: 2.x , fix46 , and master .
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmzu1mzuyoq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "alt=" here to write a picture descriptive narrative "title=" ">
To view the rxpermissions that support RXJAVA2, switch the branch to 2.x, for example, the following:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmzu1mzuyoq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "alt=" here to write a picture descriptive narrative "title=" ">
3. rxpermissions code Download
Download com.tbruyelle.rxpermissions the code with the package name (Support rxjava1.x):
clone https://github.com/tbruyelle/RxPermissions
Download com.tbruyelle.rxpermissions2 the code with the package name (Support RXJAVA2):
clone https://github.com/tbruyelle/RxPermissions RxPermissions2 -b 2.x
4. Precautions for use by Rxpermissions
Refer to the Readme in Https://github.com/tbruyelle/RxPermissions.
(1) minSdkVersion must be >= 11.
(2) using Rxpermissions to request permission, must Activity.onCreate() be View.onFinishInflate() processed in or.
cannot be onResume() processed in. Because Onresume () can perform very frequently in the life cycle of the app. Assume that you are requesting permission. The app starts again (such as a screen rotation that causes the app to close and then create again). Then the user's choice (consent or refusal) will not be sent to the app. Many other discussions, please refer to: https://github.com/tbruyelle/RxPermissions/issues/69
5. Rxpermissions Use
Based on RXJAVA2, use com.tbruyelle.rxpermissions2 the rxpermissions with the package name.
5.1 App module's Build.gradle
dependencies { ... ‘io.reactivex.rxjava2:rxandroid:2.0.1‘ ‘io.reactivex.rxjava2:rxjava:2.0.5‘ ‘com.tbruyelle.rxpermissions2:rxpermissions:[email protected]‘ ...}
5.2 Use permissions in Androidmanifest.xml
<!--protection level is dangerous, need request runtime permission-- <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/ > <uses-permission android:name="Android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="Android.permission.READ_PHONE_STATE"/> <uses-permission android:name="Android.permission.READ_CALL_LOG"/> <uses-permission android:name="Android.permission.RECORD_AUDIO"/> <uses-permission android:name="Android.permission.CAMERA"/> <uses-permission android:name="Android.permission.GET_ACCOUNTS"/> <uses-permission android:name="Android.permission.READ_CONTACTS"/> <uses-permission android:name="Android.permission.READ_CALENDAR"/> <uses-permission android:name="Android.permission.SEND_SMS"/> <uses-permission android:name="Android.permission.READ_SMS"/> <uses-permission android:name="Android.permission.CALL_PHONE"/>
5.3 Requesting permission in the activity's OnCreate ()
PackageCom.galian.rxjavatest;ImportAndroid. Manifest;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportCom.tbruyelle.rxpermissions2.Permission;ImportCom.tbruyelle.rxpermissions2.RxPermissions;ImportIo.reactivex.functions.Consumer; Public class rxpermissiontestactivity extends appcompatactivity { Private Static FinalString TAG ="Rxpermissiontest";@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.activity_rx_permission_test); Requestpermissions (); }Private void requestpermissions() {Rxpermissions rxpermission =NewRxpermissions (rxpermissiontestactivity. This); Rxpermission Requesteach (Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permiss Ion. Write_external_storage, Manifest.permission.READ_CALENDAR, Manifest.permissi On. Read_call_log, Manifest.permission.READ_CONTACTS, Manifest.permission.READ_P Hone_state, Manifest.permission.READ_SMS, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA, Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS). Subscribe (NewConsumer<permission> () {@Override Public void Accept(Permission Permission)throwsException {if(permission.granted) {//user has consented to this permissionLOG.D (TAG, Permission.name +"is granted."); }Else if(Permission.shouldshowrequestpermissionrationale) {//The user denied the permission, did not check "no longer ask" (never ask again), then the next time you start. A dialog box is also prompted to request permissionLOG.D (TAG, Permission.name +"is denied. More info should is provided. "); }Else{//The user denied the permission, and checked "Do not ask again"LOG.D (TAG, Permission.name +"is denied."); } } }); }}
5.4 Interface display and log
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmzu1mzuyoq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "alt=" here to write a picture descriptive narrative "title=" ">
Suppose you click "Reject". Do not select "Do not ask again", log is:
isbeprovided.
Suppose you click "Reject" and select "Don't ask again." The log is:
D/RxPermissionTestandroid.permission.ACCESS_FINE_LOCATION is denied.
Suppose you click "Agree" and log is:
D/RxPermissionTestandroid.permission.ACCESS_FINE_LOCATION is granted.
6. References
- (1) https://github.com/tbruyelle/RxPermissions
- (2) https://developer.android.com/guide/topics/permissions/requesting.html
- (3) https://developer.android.com/training/permissions/requesting.html
Using Rxpermissions (based on RXJAVA2)