携帯電話の起動権限がなければ、携帯電話の設定画面にジャンプしないかどうかを判断します.
問題のように・・・コードを記録します
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.functions.Function;
import io.reactivex.subjects.PublishSubject;
/**
* author:luck
*/
public class RxPermissions {
public static final String TAG = "RxPermissions";
public static final Object TRIGGER = new Object();
public RxPermissionsFragment mRxPermissionsFragment;
public RxPermissions(@NonNull Activity activity) {
mRxPermissionsFragment = getRxPermissionsFragment(activity);
}
private RxPermissionsFragment getRxPermissionsFragment(Activity activity) {
RxPermissionsFragment rxPermissionsFragment = null;
try {
rxPermissionsFragment = findRxPermissionsFragment(activity);
boolean isNewInstance = rxPermissionsFragment == null;
if (isNewInstance) {
rxPermissionsFragment = new RxPermissionsFragment();
FragmentManager fragmentManager = activity.getFragmentManager();
fragmentManager
.beginTransaction()
.add(rxPermissionsFragment, TAG)
.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
}
} catch (Exception e) {
e.printStackTrace();
}
return rxPermissionsFragment;
}
private RxPermissionsFragment findRxPermissionsFragment(Activity activity) {
return (RxPermissionsFragment) activity.getFragmentManager().findFragmentByTag(TAG);
}
public void setLogging(boolean logging) {
mRxPermissionsFragment.setLogging(logging);
}
/**
* Map emitted items from the source observable into {@code true} if permissions in parameters
* are granted, or {@code false} if not.
*
* If one or several permissions have never been requested, invoke the related framework method
* to ask the user if he allows the permissions.
*/
@SuppressWarnings("WeakerAccess")
public ObservableTransformer ensure(final String... permissions) {
return new ObservableTransformer() {
@Override
public ObservableSource apply(Observable o) {
return request(o, permissions)
// Transform Observable to Observable
.buffer(permissions.length)
.flatMap(new Function, ObservableSource>() {
@Override
public ObservableSource apply(List permissions) throws Exception {
if (permissions.isEmpty()) {
// Occurs during orientation change, when the subject receives onComplete.
// In that case we don't want to propagate that empty list to the
// subscriber, only the onComplete.
return Observable.empty();
}
// Return true if all permissions are granted.
for (Permission p : permissions) {
if (!p.granted) {
return Observable.just(false);
}
}
return Observable.just(true);
}
});
}
};
}
/**
* Map emitted items from the source observable into {@link Permission} objects for each
* permission in parameters.
*
* If one or several permissions have never been requested, invoke the related framework method
* to ask the user if he allows the permissions.
*/
@SuppressWarnings("WeakerAccess")
public ObservableTransformer ensureEach(final String... permissions) {
return new ObservableTransformer() {
@Override
public ObservableSource apply(Observable o) {
return request(o, permissions);
}
};
}
/**
* Request permissions immediately, must be invoked during initialization phase
* of your application.
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public Observable request(final String... permissions) {
return Observable.just(TRIGGER).compose(ensure(permissions));
}
/**
* Request permissions immediately, must be invoked during initialization phase
* of your application.
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public Observable requestEach(final String... permissions) {
return Observable.just(TRIGGER).compose(ensureEach(permissions));
}
private Observable request(final Observable> trigger, final String... permissions) {
if (permissions == null || permissions.length == 0) {
throw new IllegalArgumentException("RxPermissions.request/requestEach requires at least one input permission");
}
return oneOf(trigger, pending(permissions))
.flatMap(new Function
使うのもかなり简単で、実现についてコードの书き込みをします.初期:
private RxPermissions rxPermissions;
oncreate ·
rxPermissions = new RxPermissions(this);
参照:
private void checkLocation1() {
// ( )
rxPermissions.request(Manifest.permission.ACCESS_COARSE_LOCATION)
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Boolean aBoolean) {
if (aBoolean) {//aBoolean 。true
checkLocation2();// ··
} else {
showToast(getString(R.string.request_permission_location));
getAppDetailSettingIntent();//
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
上記の部分では,ある権限の個別判断を紹介し,より多くの権限判断が必要であればonNextでboolean判断方法で書く.ここでもう一つの方法を類推します.checkLocation 2()
//
private void checkLocation2() {
rxPermissions.request(Manifest.permission.ACCESS_FINE_LOCATION)
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Boolean aBoolean) {
if (aBoolean) {
checkWritePermission();//
} else {
showToast(getString(R.string.request_permission_location));
getAppDetailSettingIntent();//
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
インタフェースのジャンプを設定すると、一部の携帯電話しかジャンプできません.また、カスタマイズされた携帯電話は単独で処理する必要があります.
/**
* 。
*/
// TODO: ,
private void getAppDetailSettingIntent(){
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if(Build.VERSION.SDK_INT >= 9){
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", getPackageName(), null));
} else if(Build.VERSION.SDK_INT <= 8){
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
intent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
}
startActivity(intent);
}
詳細コードはgiを参照thubhttps://github.com/BINBINXIAO/RxPermisson