gpt4 book ai didi

android - wifi 提供商 onLocationChanged 从未被调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:18:01 24 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。我使用 wifi 来检索用户位置。我已经在几部手机上进行了测试;在其中一些上,我永远无法获得位置更新。似乎从未调用过 onLocationChanged 方法。我的代码附在下面..任何建议表示赞赏!!!

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

LocationManager locationManager;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

String provider = locationManager.NETWORK_PROVIDER;
Location l = locationManager.getLastKnownLocation(provider);

updateWithNewLocation(l);

locationManager.requestLocationUpdates(provider, 0, 0,
locationListener);
}

private void updateWithNewLocation(Location location) {
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);

String latLongString = "No location found";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
}

myLocationText.setText("Your Current Position is:\n" +
latLongString);
}

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
Log.i("onLocationChanged", "onLocationChanged");
Toast.makeText(getApplicationContext(), "location changed",
Toast.LENGTH_SHORT).show();
}

public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,
Bundle extras) {}

};

list

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

最佳答案

我的猜测是,您无法从中获取位置更新的手机没有正确的数据连接。网络提供商需要 GSM/3G 或 WiFi 数据连接,才能使用手机的手机/wifi 数据从 Google 服务器检索定位信息。您可以使用以下方法进行检查。

public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;

关于android - wifi 提供商 onLocationChanged 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11183064/

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