我是Android的新手,我想在按钮点击中获取当前位置。我正在关注this documentation。单击相应按钮时,我正在调用public void showLocation(View v)
。我已经尝试过一块一块来测试它。以下是我的调查。
要测试函数调用和权限检查, 我收到来自以下函数的GRANTED消息
public void showLocation(View v) {
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if( permissionCheck != PackageManager.PERMISSION_GRANTED ) {
Toast.makeText(getApplicationContext(), "Granted", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Permission Not Granted", Toast.LENGTH_SHORT).show();
}
}
要测试
mFusedLocationClient.getLastLocation().addOnSuccessListener
, 我的申请不幸在此停止
public void showLocation(View v) {
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if( permissionCheck != PackageManager.PERMISSION_GRANTED ) {
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
Toast.makeText(getApplicationContext(), "hello there", Toast.LENGTH_SHORT).show();
}
});
}
else {
Toast.makeText(getApplicationContext(), "Permission Not Granted", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
你做错了。首先 。想想你写的这个条件。
if( permissionCheck != PackageManager.PERMISSION_GRANTED )
因此,如果未授予许可,则您要求提供您的位置。
而不是你应该从用户那里请求权限。
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
并且在此您可以获得结果
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! do that
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'switch' lines to check for other
// permissions this app might request
}
}
所以你只需要正确获得运行时权限。没有别的
编辑:根据您的评论您可以执行以下操作。
yourbutton .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//First of All check for permission .
askForPermission(Manifest.permission.ACCESS_FINE_LOCATION, READ_EXST);
}
});
private void askForPermission(String permission, Integer requestCode) {
if (ContextCompat.checkSelfPermission(UserActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(UserActivity.this, permission)) {
//This is called if user has denied the permission before
//In this case I am just asking the permission again
ActivityCompat.requestPermissions(UserActivity.this, new String[]{permission}, requestCode);
} else {
ActivityCompat.requestPermissions(UserActivity.this, new String[]{permission}, requestCode);
}
} else {
//FETCH USER LOCATION HERE
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
//FETCH USER LOCATION HERE
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}