为啥权限 Manifest.permission.ACCESS_FINE_LOCATION 没有激活“位置”选项?
Posted
技术标签:
【中文标题】为啥权限 Manifest.permission.ACCESS_FINE_LOCATION 没有激活“位置”选项?【英文标题】:Why the permission Manifest.permission.ACCESS_FINE_LOCATION not active the "Location" option?为什么权限 Manifest.permission.ACCESS_FINE_LOCATION 没有激活“位置”选项? 【发布时间】:2020-09-01 07:16:07 【问题描述】:我有一个为用户发出位置请求的代码,只要用户已经处于“位置”模式,一切正常,但是,如果“位置”模式未在设备通知栏中激活location 返回 null,我知道当我们禁用该选项时,位置缓存被清除,我怎么能要求用户激活 q 的选项然后我请求 getLastLocation?
fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>()
@Override
public void onComplete(@NonNull Task<Location> task)
localidade = task.getResult();
if(localidade != null)
try
locFromLatLon = new Geocoder(AddAnuncioActivity.this, Locale.getDefault());
List<Address> addresses = locFromLatLon.getFromLocation(localidade.getLatitude(), localidade.getLongitude(), 1);
Log.i("VALORZAS", addresses.get(0).getCountryName());
catch (IOException e)
e.printStackTrace();
else
// Put some here whenlocation is null -- this happens when the Location option in notification bar of // user is unabled
);
这仅在图像中的选项启用时才有效,如果此选项关闭,则 getResult 返回 null。
我的 onResume 函数:
@Override
protected void onResume()
super.onResume();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(60000);
locationRequest.setFastestInterval(5000);
LocationCallback locationCallback = new LocationCallback()
@Override
public void onLocationResult(LocationResult locationResult)
if (locationResult == null)
Toast.makeText(getApplicationContext(), "Localidade ainda vazia", Toast.LENGTH_LONG).show();
return;
for (Location location : locationResult.getLocations())
;
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
【问题讨论】:
也许可以试试 LocationManager ? 怎么样?你有例子吗? 【参考方案1】:你可以试试LocationManager
:
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
//Call Alert or use android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS
参考Documentation
【讨论】:
在这种情况下,我应该明确声明位置提供者吗? 不,你不需要这样做。 好的,我现在就试试。以上是关于为啥权限 Manifest.permission.ACCESS_FINE_LOCATION 没有激活“位置”选项?的主要内容,如果未能解决你的问题,请参考以下文章