アクティビティが画面に表示されるたびにユーザの位置を要求したい(私の唯一のオプションはアクティビティのonResume()
メソッドだと思います) android 6の新しいランタイム許可モデルをサポートしたいと思います。実行時に `onResume()`を実行する際に、アンドロイド6以上でpermissionDispatcherライブラリを使用する方法
私はpermissionDispatcherライブラリを使用しており、完全に動作します。問題はthis issue suggestsです。onResume()
メソッドでアクセス権を要求する関数を呼び出し、ユーザーがそのアクセス権を拒否すると、無限ループに陥ります。
しかし私の問題は、アクティビティのすべての外観でユーザーの場所を取得したいので、onResume()
メソッドを使用する以外の方法は考えられません(したがって、ユーザーが無限ループに陥ってしまう許可を拒否する)。
はここに私のコードのサンプルです:
@NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION)
void startLocationUpdate() {
mLocationLoadingDialog.show();
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@OnShowRationale(Manifest.permission.ACCESS_FINE_LOCATION)
void showRationalForFineLocation(final PermissionRequest request) {
new AlertDialog.Builder(this)
.setMessage("this app needs your permission to get your location")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
request.proceed();
}
})
.setNegativeButton("no way", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
request.cancel();
exitApp();
}
})
.setCancelable(false)
.show();
}
@OnPermissionDenied(Manifest.permission.ACCESS_FINE_LOCATION)
void showDeniedForFINELOCATION() {
exitApp();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
RequestSelectorActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
private void exitApp() {
new AlertDialog.Builder(this)
.setCancelable(false)
.setMessage("you didn't allow us to know where you are, so the application will exit")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
}).show();
}
私は私の状況のために何ができるのでしょうか?