显示允许应用访问设备的位置
Displaying Allow app to access device's location
我是 android 编程新手,我需要显示警告,以便用户在 he/she 单击允许按钮时自动授予我的应用程序权限。
喜欢这个应用程序
但实际上我的代码会显示一条消息,将用户带到设置中,因此 he/she 可以手动更改它,但并不是每个人都知道如何手动更改
我怎样才能获得第一个警报,以便应用程序自动获得权限,而不是我目前的方式,它只是将用户移动到设置页面,他们必须手动完成
这是我的代码
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(_activity);
alertDialog.setTitle("GPS Settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu ?");
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
_activity.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
您好,这是一个 运行 时间许可,只是通过此 link 以供参考 click here
还要检查这个
为了保护用户,他们必须在 运行 时获得授权,以便用户知道这是否与他的行为有关。
为此:
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
它将显示一个对话框,用户可以在其中选择是否将您的应用程序授权为使用位置信息。
然后使用这个函数获取用户答案:
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
如果用户接受一次,那么您的应用程序将记住它,您将不再需要发送此 DialogBox。请注意,如果用户决定,他可以稍后将其禁用。然后在请求位置之前,您必须测试权限是否仍然被授予:
public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
您必须如下请求用户的许可
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
这里是参考link
允许您的应用访问设备的位置(第一个屏幕截图)与启用 GPS(第二个屏幕截图)不同。您实际上需要两者:首先检查您是否像其他答案所建议的那样获得许可,然后检查是否像您已经在做的那样启用了 GPS。
如果要以编程方式启用 GPS,则需要使用 "Settings API"。查看 如何实施它。
我是 android 编程新手,我需要显示警告,以便用户在 he/she 单击允许按钮时自动授予我的应用程序权限。
喜欢这个应用程序
但实际上我的代码会显示一条消息,将用户带到设置中,因此 he/she 可以手动更改它,但并不是每个人都知道如何手动更改
我怎样才能获得第一个警报,以便应用程序自动获得权限,而不是我目前的方式,它只是将用户移动到设置页面,他们必须手动完成
这是我的代码
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(_activity);
alertDialog.setTitle("GPS Settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu ?");
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
_activity.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
您好,这是一个 运行 时间许可,只是通过此 link 以供参考 click here
还要检查这个
为了保护用户,他们必须在 运行 时获得授权,以便用户知道这是否与他的行为有关。
为此:
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
它将显示一个对话框,用户可以在其中选择是否将您的应用程序授权为使用位置信息。
然后使用这个函数获取用户答案:
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
如果用户接受一次,那么您的应用程序将记住它,您将不再需要发送此 DialogBox。请注意,如果用户决定,他可以稍后将其禁用。然后在请求位置之前,您必须测试权限是否仍然被授予:
public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
您必须如下请求用户的许可
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
这里是参考link
允许您的应用访问设备的位置(第一个屏幕截图)与启用 GPS(第二个屏幕截图)不同。您实际上需要两者:首先检查您是否像其他答案所建议的那样获得许可,然后检查是否像您已经在做的那样启用了 GPS。
如果要以编程方式启用 GPS,则需要使用 "Settings API"。查看