Java Android位置的多个运行时权限
在我的示例应用程序中,我从用户那里获取位置,并使用Google Play服务和FusedLocationApi。我还试图实现位置的运行时权限,我正在寻找最佳实践。我已经找了一整天了,我找到了这样的解决方案。我的Java Android位置的多个运行时权限,java,android,android-permissions,android-location,runtime-permissions,Java,Android,Android Permissions,Android Location,Runtime Permissions,在我的示例应用程序中,我从用户那里获取位置,并使用Google Play服务和FusedLocationApi。我还试图实现位置的运行时权限,我正在寻找最佳实践。我已经找了一整天了,我找到了这样的解决方案。我的PermissionUtils类中有这两个方法 public static void askPermissions(final Activity activity) { if (isRuntimePermissionRequired()) { if (Context
PermissionUtils
类中有这两个方法
public static void askPermissions(final Activity activity) {
if (isRuntimePermissionRequired()) {
if (ContextCompat.checkSelfPermission(activity, FINE_LOCATION) != GRANTED || ContextCompat.checkSelfPermission(activity, COARSE_LOCATION) != GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, FINE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale(activity, COARSE_LOCATION)) {
new AlertDialog.Builder(activity)
.setTitle("Permission required")
.setMessage("Location is required for this application to work ! ")
.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
activity.finish();
}
})
.show();
} else {
if (getApplicationLaunchedFirstTime(activity)) {
setApplicationLaunchedFirstTime(activity);
ActivityCompat.requestPermissions(activity, PERMISSIONS_LOCATION, REQUEST_LOCATION);
} else {
new AlertDialog.Builder(activity)
.setTitle("Permission Disabled")
.setMessage("Please enable the permission in \n Settings>Uber>Permission \n and check 'location' permission")
.setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
activity.startActivity(new Intent(Settings.ACTION_SETTINGS));
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
activity.finish();
}
})
.show();
}
}
}
}
}
public static boolean isPermissionsGranted(Context context) {
if (ActivityCompat.checkSelfPermission(context, FINE_LOCATION) == GRANTED && ActivityCompat.checkSelfPermission(context, COARSE_LOCATION) == GRANTED) {
return true;
}
return false;
}
在我的活动中
我在onConnected
中使用这些方法,如下所示
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "onConnected");
if (PermissionUtils.isPermissionsGranted(this)) {
try {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
handleNewLocation(location);
}
} catch (SecurityException ex) {
Log.e(TAG, "SecurityException caught" + ex);
}
} else {
PermissionUtils.askPermissions(this);
}
}
但应用程序一直背对背地调用onConnected
,直到我授予允许权限。我做错了什么?在Android中实现权限的最佳实践是什么?任何帮助都将不胜感激。请参阅,何时调用
onConnected()
?调用mgoogleapclient.connect()
对吗
因此,只有在授予权限的情况下才调用它。因此,如果未授予您权限,则不会调用onConnected()
这样,您将仅在授予权限时请求位置更新。仅在授予权限时请求位置更新。如果没有,则仅当已授予权限时才请求权限并触发位置更新,在
OnRequestPermissionResult()
@向导中,您的意思是仅当授予权限时,我才需要cal onConnected和onLocationChanged?完全正确。一旦获得权限,请求位置客户端granted@Wizard但这些都是回调方法。这怎么可能?bhai,何时调用onConnected()
?调用mgoogleapclient.connect()
对吗?因此,如果许可被授予,请调用。因此,如果未授予您权限,则不会调用onConnected()
。