EasyPermission 的使用方式 记录一下
2018-09-04 本文已影响64人
小于先森
一切尽在代码中
package rjh.yilin.ui.activity;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import java.io.File;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;
import rjh.yilin.R;
import rjh.yilin.app.AppConstants;
import rjh.yilin.app.AppManager;
import rjh.yilin.base.BaseActivity;
import rjh.yilin.utils.LogUtils;
import rjh.yilin.utils.YiLinUtils;
/**
* Created by 于德海 on 2018/8/30.
* package rjh.yilin.ui.activity
* email : yudehai0204@163.com
*
* @describe
*/
public class FirstActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
//要请求的权限
private String permissions[] = new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_NETWORK_STATE
};
@Override
protected void initParam(Bundle param) {
}
@Override
protected int getLayoutId() {
return R.layout.aty_first;
}
@Override
protected void initView() {
if (EasyPermissions.hasPermissions(this, permissions)) {//判断是否拥有权限
goMain();//获取权限后进行页面跳转
} else {
//请求权限
EasyPermissions.requestPermissions(this, "请求必要的权限,拒绝权限可能会无法使用app", 0, permissions);
}
}
@Override
protected void initData() {
}
@Override
protected void initListener() {
}
@Override
public void onClick(View v) {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);//将第三方权限框架与activity绑定
}
private void goMain() {
File file = new File(AppConstants.FILE_DIR);
if(!file.exists()){
file.mkdir();
}
Observable.create(new ObservableOnSubscribe<Object>() {
@Override
public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
Thread.sleep(2000);
emitter.onComplete();
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Object>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Object o) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
startActivity(new Intent(FirstActivity.this, MainActivity.class));
YiLinUtils.DelaycloseActivity(FirstActivity.this);
}
});
}
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {//权限全部同意
LogUtils.e("成功" + perms.size());
goMain();
}
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {//拒绝了部分权限
LogUtils.e("失败");
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {//拒绝权限且点击了不再提示
new AppSettingsDialog.Builder(this).build().show();//跳转应用设置页面
} else {
EasyPermissions.requestPermissions(this, "请求必要的权限,拒绝权限可能会无法使用app", 1, perms.toArray(new String[perms.size()]));
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//当从软件设置界面,返回当前程序时候
case AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE:
if (EasyPermissions.hasPermissions(this, permissions)) {
goMain();
} else {
AppManager.getInstance().AppExit();
}
break;
}
}
}