位置侦听器适用于 Android 6.0 但不适用于 android 8.0+ [关闭]
Posted
技术标签:
【中文标题】位置侦听器适用于 Android 6.0 但不适用于 android 8.0+ [关闭]【英文标题】:Location Listener works in Android 6.0 but not android 8.0+ [closed] 【发布时间】:2021-01-11 22:44:12 【问题描述】:我正在学习如何在应用程序中使用地理定位,我遇到了这个问题,我可以在模拟 android 6.0 时获取用户的位置,但是当我尝试在 Android 8.0 中运行相同的确切代码时,我没有得到用户的位置。老实说,我不明白我错在哪里或为什么它不起作用。这是我的代码:
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener()
@Override
public void onLocationChanged(@NonNull Location location)
Log.i("Location", location.toString());
;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
//Ask for permission
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.ACCESS_FINE_LOCATION, 1);
【问题讨论】:
【参考方案1】:在检查好位置的权限后,我通过添加这个 else 语句设法解决了我的问题。
代码如下:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
//Ask for permission
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.ACCESS_FINE_LOCATION, 1);
else
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
原因:
似乎 locationUpdate 仅适用于我的应用程序的第一次尝试,因为我只在 onRequestPermissionResult 代码块中设置了我的 locationUpdate。我的 onRequesPermissionResult 仅在我请求权限时运行,并且在我下次运行应用程序时,我已经获得了用户的权限,所以在代码中,我正在检查权限,这将返回 false,因为我已经获得了权限并且我如果已授予权限,则未设置 locationUpdate。
所以我尝试在上面添加 else 语句。
简化问题:
我第一次打开应用时,我的应用还没有获得该位置的权限(ACCESS_FINE_LOCATION)。
我的权限检查行将返回 true 并且您请求权限。
onRequestPermissionResult 将运行,您编写代码以在其中设置 locationUpdate 并且它起作用了。
-下次我打开应用时,我的应用已经拥有该位置的权限。
我的权限检查行将返回false,它不会执行范围内的代码,因此没有执行请求权限。
我的位置没有 locationUpdate,因为 onReuqestionPermissionResult 方法没有执行。
解决方案:
如果检查权限返回false,我设置了条件,这意味着权限已经被授予,所以我不必请求权限,只需为您的locationManager设置locationUpdate即可。【讨论】:
以上是关于位置侦听器适用于 Android 6.0 但不适用于 android 8.0+ [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
IBM Worklight 6.0 - 无法在 avd 上使用适用于 android 环境的 dojo 工具包运行示例混合 Worklight 应用程序?
MTK Recovery 模式横屏修改(适用于6.0 + 8.1)
如何将 Firebase 数据检索到 TableView android 中?我应该使用 RecycleView 还是仅适用于列表?