gpt4 book ai didi

android - onLocationChanged 从未调用过?

转载 作者:行者123 更新时间:2023-11-30 02:38:46 27 4
gpt4 key购买 nike

我使用的是精确的代码形式 developer.android.com/但它似乎不是 100% 有效,我昨天使用相同的代码并且有效。我拥有所需的所有权限。

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

我已经添加了 ACCESS_COARSE_LOCATION 权限以防万一首先不起作用。

代码:

Log.i("DEV", "LocationManager");
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

Log.i("DEV", "LocationListener");
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {Log.i("DEV", "onLocationChanged");}

public void onStatusChanged(String provider, int status, Bundle extras) {Log.i("DEV", "onStatusChanged");}

public void onProviderEnabled(String provider) {Log.i("DEV", "onProviderEnabled");}

public void onProviderDisabled(String provider) {Log.i("DEV", "onProviderDisabled");}
};

Log.i("DEV", "Register listener");
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

我打开了移动数据或 wifi 总是相同的日志输出:

09-28 20:06:56.816  21408-21408/smst.dev.mav.com I/DEV﹕ LocationManager
09-28 20:06:56.826 21408-21408/smst.dev.mav.com I/DEV﹕ LocationListener
09-28 20:06:56.826 21408-21408/smst.dev.mav.com I/DEV﹕ Register listener
09-28 20:06:56.826 21408-21408/smst.dev.mav.com I/DEV﹕ onProviderDisabled

谢谢!

最佳答案

Android 有两个位置提供程序 - GPS 和粗略(网络)位置。您需要分别启用两者才能使用每一个。看看这里:

enter image description here

您可以使用以下方法检查它们是否已启用:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

关于android - onLocationChanged 从未调用过?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087952/

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