请求位置权限仅在应用程序中断时出现
Asking for location permission only appears when app is interrupted
我正在开发一个使用 Google 地图的 android 应用程序。
预期行为是:
- 进入地图activity
- 要求用户使用他的位置
- 显示附近的地点
当我 运行 在 Android Studio 中使用模拟器的应用程序时,流程是预期的 (Pixel 2 Api 30),但是当我 运行它在我的 phone 上,只有在我停止应用程序 运行.
后才会请求许可
为什么会这样?
活跃度
public class NearbyPharmaciesActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final int MY_PERMISSION_CODE = 1000;
private GoogleMap mMap;
private double latitude, longitude;
private Location lastLocation;
private Marker marker;
private GoogleMapsApi mService;
private FusedLocationProviderClient fusedLocationProviderClient;
private LocationCallback locationCallback;
private LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby_pharmacies);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// Init service
mService = RetrofitGoogleMaps.getApi();
// Request runtime permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_pharmacy:
nearbyPlaces("pharmacy");
break;
default:
break;
}
return true;
}
});
buildLocationCallBack();
buildLocationRequest();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, locationCallback, Looper.myLooper());
}
private boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
}, MY_PERMISSION_CODE);
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
}, MY_PERMISSION_CODE);
}
return false;
} else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSION_CODE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
buildLocationCallBack();
buildLocationRequest();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, locationCallback, Looper.myLooper());
}
}
}
break;
}
}
private void buildLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(300000);
mLocationRequest.setFastestInterval(10000);
mLocationRequest.setSmallestDisplacement(30);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void buildLocationCallBack() {
System.out.println("Reached here");
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
lastLocation = locationResult.getLastLocation();
System.out.println("last location: " + lastLocation.toString());
if (marker != null) {
marker.remove();
}
latitude = lastLocation.getLatitude();
longitude = lastLocation.getLongitude();
System.out.println("LATITUDE: " + latitude);
System.out.println("LONGITUDE: " + longitude);
LatLng latLng = new LatLng(latitude, longitude);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("Your position")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
marker = mMap.addMarker(options);
//LocationData locationData = new LocationData(latitude, longitude);*/
//mDatabase.child("location").child(userId).child(String.valueOf(new Date().getTime())).setValue(locationData);
//getMarkers();
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
};
}
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
mMap.setMyLocationEnabled(true);
}
}
}
private void nearbyPlaces(final String placeType) {
String url = getUrl(latitude, longitude, placeType);
System.out.println("URL: "+ url);
mService.getNearByPlaces(url)
.enqueue(new Callback<SearchResponse>() {
@Override
public void onResponse(Call<SearchResponse> call, Response<SearchResponse> response) {
if (response.isSuccessful()) {
System.out.println("Sucesso");
for (int i = 0; i < response.body().getResults().length; i++) {
System.out.println("ENTRA NO IF");
MarkerOptions markerOptions = new MarkerOptions();
Result googlePlace = response.body().getResults()[i];
double lat = Double.parseDouble(googlePlace.getGeometry().getLocation().getLat());
double lng = Double.parseDouble(googlePlace.getGeometry().getLocation().getLng());
String placeName = googlePlace.getName();
String vicinity = googlePlace.getVicinity();
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
markerOptions.title(placeName);
if (placeType.equals("pharmacy")) {
System.out.println("BEM");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
} else {
System.out.println("CONA");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
}
mMap.addMarker(markerOptions);
mMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
}
}
@Override
public void onFailure(Call<SearchResponse> call, Throwable t) {
}
});
}
private String getUrl(double latitude, double longitude, String placeType) {
StringBuilder builder = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
builder.append("location=" + latitude + "," + longitude);
builder.append("&radius=10000");
builder.append("&type=" + placeType);
builder.append("&sensor=true");
builder.append("&key=" + "XXXXXXXXXXXXXXXXXXXXXXXX");
Log.d("getUrl", builder.toString());
return builder.toString();
}
}
找到问题了。
发生这种情况是因为我在两个不同的场合请求了权限。
首先,我会在我的 Main Activity 中请求 SMS 权限,然后,当输入此 Activity 时,我会请求位置权限。
在 android 开发中,您必须立即请求所有权限。
我正在开发一个使用 Google 地图的 android 应用程序。 预期行为是:
- 进入地图activity
- 要求用户使用他的位置
- 显示附近的地点
当我 运行 在 Android Studio 中使用模拟器的应用程序时,流程是预期的 (Pixel 2 Api 30),但是当我 运行它在我的 phone 上,只有在我停止应用程序 运行.
后才会请求许可为什么会这样?
活跃度
public class NearbyPharmaciesActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final int MY_PERMISSION_CODE = 1000;
private GoogleMap mMap;
private double latitude, longitude;
private Location lastLocation;
private Marker marker;
private GoogleMapsApi mService;
private FusedLocationProviderClient fusedLocationProviderClient;
private LocationCallback locationCallback;
private LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby_pharmacies);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// Init service
mService = RetrofitGoogleMaps.getApi();
// Request runtime permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_pharmacy:
nearbyPlaces("pharmacy");
break;
default:
break;
}
return true;
}
});
buildLocationCallBack();
buildLocationRequest();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, locationCallback, Looper.myLooper());
}
private boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
}, MY_PERMISSION_CODE);
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
}, MY_PERMISSION_CODE);
}
return false;
} else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSION_CODE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
buildLocationCallBack();
buildLocationRequest();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, locationCallback, Looper.myLooper());
}
}
}
break;
}
}
private void buildLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(300000);
mLocationRequest.setFastestInterval(10000);
mLocationRequest.setSmallestDisplacement(30);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void buildLocationCallBack() {
System.out.println("Reached here");
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
lastLocation = locationResult.getLastLocation();
System.out.println("last location: " + lastLocation.toString());
if (marker != null) {
marker.remove();
}
latitude = lastLocation.getLatitude();
longitude = lastLocation.getLongitude();
System.out.println("LATITUDE: " + latitude);
System.out.println("LONGITUDE: " + longitude);
LatLng latLng = new LatLng(latitude, longitude);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("Your position")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
marker = mMap.addMarker(options);
//LocationData locationData = new LocationData(latitude, longitude);*/
//mDatabase.child("location").child(userId).child(String.valueOf(new Date().getTime())).setValue(locationData);
//getMarkers();
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
};
}
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
mMap.setMyLocationEnabled(true);
}
}
}
private void nearbyPlaces(final String placeType) {
String url = getUrl(latitude, longitude, placeType);
System.out.println("URL: "+ url);
mService.getNearByPlaces(url)
.enqueue(new Callback<SearchResponse>() {
@Override
public void onResponse(Call<SearchResponse> call, Response<SearchResponse> response) {
if (response.isSuccessful()) {
System.out.println("Sucesso");
for (int i = 0; i < response.body().getResults().length; i++) {
System.out.println("ENTRA NO IF");
MarkerOptions markerOptions = new MarkerOptions();
Result googlePlace = response.body().getResults()[i];
double lat = Double.parseDouble(googlePlace.getGeometry().getLocation().getLat());
double lng = Double.parseDouble(googlePlace.getGeometry().getLocation().getLng());
String placeName = googlePlace.getName();
String vicinity = googlePlace.getVicinity();
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
markerOptions.title(placeName);
if (placeType.equals("pharmacy")) {
System.out.println("BEM");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
} else {
System.out.println("CONA");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
}
mMap.addMarker(markerOptions);
mMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
}
}
@Override
public void onFailure(Call<SearchResponse> call, Throwable t) {
}
});
}
private String getUrl(double latitude, double longitude, String placeType) {
StringBuilder builder = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
builder.append("location=" + latitude + "," + longitude);
builder.append("&radius=10000");
builder.append("&type=" + placeType);
builder.append("&sensor=true");
builder.append("&key=" + "XXXXXXXXXXXXXXXXXXXXXXXX");
Log.d("getUrl", builder.toString());
return builder.toString();
}
}
找到问题了。
发生这种情况是因为我在两个不同的场合请求了权限。 首先,我会在我的 Main Activity 中请求 SMS 权限,然后,当输入此 Activity 时,我会请求位置权限。
在 android 开发中,您必须立即请求所有权限。