gpt4 book ai didi

Android:没有locationListener的用户位置

转载 作者:行者123 更新时间:2023-11-29 02:11:11 26 4
gpt4 key购买 nike

我想知道是否可以在不使用 LocationListener 的情况下获取用户位置。

我问的原因是我的 locationListener 事件没有被调用。

public class LocationActivity extends Activity implements LocationListener{

private LocationManager m_locationManager;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

LinearLayout content = new LinearLayout(this);
content.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
content.setOrientation(LinearLayout.VERTICAL);
content.setBackgroundColor(Color.TRANSPARENT);

TextView infoLabel = new TextView(this);
infoLabel.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
infoLabel.setTextSize(20.0f);
infoLabel.setText("Initializing...");
content.addView(infoLabel);

try{
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

String provider = null;

if ( m_locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
provider = LocationManager.GPS_PROVIDER ;
Log.d("Unity", "Using GPS");
m_locationManager.requestLocationUpdates(provider, 0, 0, this);
} else if(m_locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
Log.d("Unity", "Using Netword");
m_locationManager.requestLocationUpdates(provider, 0, 0, this);
} else {
Log.d("Unity", "Provider Not available");
}

}catch(Exception ex){
Log.d("Unity", "locatons error " + ex.getMessage());
}

setContentView(content);

}

public void onLocationChanged(Location location) {
Log.d("Unity", "UserLocation Lat:" + location.getLatitude() + " Long:" + location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) {Log.d("Unity", "onStatusChanged");}
public void onProviderEnabled(String provider) {Log.d("Unity", "onProviderEnabled");}
public void onProviderDisabled(String provider) {Log.d("Unity", "onProviderDisabled");}
}

查看这篇文章了解更多信息:

Android: Can't create handler inside thread that has not called Looper.prepare()

修改以反射(reflect) mmeyer 评论

最佳答案

首先,当您使用 LocationManager.getLastKnownLocation(provider) 选择提供商时,您可以检查最后已知的位置,并查看它是否不为空且不太旧。

除此之外,您的代码看起来还不错。我怀疑问题可能是您调用 requestLocationUpdates 时的第三个参数表示仅在位置变化超过 100 米时才发送更新。对于您在调试中运行并观察 logcat 的大多数情况,每秒移动一百米似乎不太可能。

来自 API 文档:

The frequency of notification may be controlled using the minTime and minDistance parameters. If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.

关于Android:没有locationListener的用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7295258/

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