android应用RxPermissions申请权限依赖添加方法和使用代码
- 环境:Android Studio2021.2,java
- 添加依赖:
打开build.gradle(app),添加以下代码:
implementation 'com.github.tbruyelle:rxpermissions:0.12'
//rxJava 注意:这里必须是rxJava3,其它不行,反正我搞了半天api 'io.reactivex.rxjava3:rxjava:3.0.4'api 'io.reactivex.rxjava3:rxandroid:3.0.0'
打开settings.gradle添加以下代码
加入前:
dependencyResolutionManagement {repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)repositories {google()mavenCentral()}
}
加入后
dependencyResolutionManagement {repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)repositories {google()mavenCentral()maven {url 'https://jitpack.io'}}
}
3.以下代码参考网上代码编写,如涉版权,即刻删除:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:id="@+id/onclick_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="权限"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.rxpermissons;import androidx.appcompat.app.AppCompatActivity;import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;import com.tbruyelle.rxpermissions3.Permission;
import com.tbruyelle.rxpermissions3.RxPermissions;import java.util.concurrent.TimeUnit;import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;public class MainActivity extends AppCompatActivity {
private RxPermissions rxPermissions;
private Context context;private static final String[] permissionsGroup = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_CALENDAR,Manifest.permission.CAMERA};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.onclick_tv).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {testEnsureEachCombined(v);}});}public void testRequest(View view) {
// 创建 RxPermissions 实例RxPermissions rxPermissions = new RxPermissions(this);rxPermissions.request(permissionsGroup).subscribe(new Observer<Boolean>() {@Overridepublic void onSubscribe(Disposable d) {}@Overridepublic void onNext(Boolean aBoolean) {if (aBoolean) {Toast.makeText(MainActivity.this, "已获取权限,可以干想干的咯", Toast.LENGTH_LONG).show();} else {//只有用户拒绝开启权限,且选了不再提示时,才会走这里,否则会一直请求开启Toast.makeText(MainActivity.this, "主人,我被禁止啦,去设置权限设置那把我打开哟", Toast.LENGTH_LONG).show();}}@Overridepublic void onError(Throwable e) {}@Overridepublic void onComplete() {}});}public void testEnsureEachCombined(View view) {RxPermissions rxPermissions = new RxPermissions(this);Observable.timer(10, TimeUnit.MILLISECONDS).compose(rxPermissions.ensureEachCombined(permissionsGroup)).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Permission>() {@Overridepublic void accept(Permission permission) throws Throwable {Log.i("cxw", "权限名称:" + permission.name + ",申请结果:" + permission.granted);}});}
}
4.运行结果:
运行后点击权限按钮,显示下图:
5.源码链接:https://download.csdn.net/download/xyufeng2008/87219396