gpt4 book ai didi

java - FusedLocationApi.getLastLocation 始终返回 null(android 6.0,api 23)

转载 作者:行者123 更新时间:2023-12-01 17:59:40 34 4
gpt4 key购买 nike

我在手机上尝试一下。它总是返回 null。为什么?(已授予所有权限。ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION)

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
Toast.makeText(MainActivity.this, "Location: "+location, Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "null", Toast.LENGTH_SHORT).show();
}

});

依赖关系:

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.android.gms:play-services-location:16.0.0'}

list :

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

最佳答案

永远无法保证您能从 getLastLocation() 获得位置。通常这意味着没有任何应用程序使用 GPS 来获取您的位置,因此不会缓存任何内容。

通常,您所要做的就是打开谷歌地图或某个位置一旦锁定就会被缓存的东西

关于java - FusedLocationApi.getLastLocation 始终返回 null(android 6.0,api 23),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60659017/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com