安卓GPS。位置侦听器不想获取修复:(
下面是一段代码,假设监听 GPS 更新并向主要活动发送通知
,但它似乎不起作用:)
没有错误,只有 onLocationChanged 没有被调用!
有趣的是:当我运行其他应用程序来修复 GPS 时,然后启动我的应用程序 => onLocationChanged 被调用,只是准确性下降,最终修复丢失。
我还尝试添加到此代码 GPS 侦听器=>它有效,每秒都会报告视野中的卫星,但从未获得修复。
到底是怎么回事? :)
public class PhoneGPSpos{
private Handler myHandler = null;
private LocationManager lm;
private LocationListener locationListener;
private Location currentBest;
Context m_context;
public PhoneGPSpos(Context context, Handler handler) {
myHandler = handler;
m_context = context;
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// Bundle bundle = new Bundle();
// boolean xtraInjection=lm.sendExtraCommand(LocationManager.GPS_PROVIDER,"force_xtra_injection",bundle);
// boolean timeInjection=lm.sendExtraCommand(LocationManager.GPS_PROVIDER,"force_time_injection",bundle);
currentBest = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
Log.w("Compass", "Loc");
if (location != null)
{
currentBest = location;
Message msg = new Message();
msg.arg1 = 5;
myHandler.sendMessage(msg);
}
}
public void onStatusChanged(String provider, int status, Bundle
extras) {
Log.d("Compass", "Stat");
}
public void onProviderEnabled(String provider) {
Log.d("Compass", "Prov e");
}
public void onProviderDisabled(String provider) {
Log.d("Compass", "Prov d");
}
};
}
public void requestGPS()
{
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Log.d("Compass", "gps requested");
}
public void stopGPS()
{
Log.d("Compass", "gps stopped");
lm.removeUpdates(locationListener);
}
public synchronized Location getBest()
{
return currentBest;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你在哪里测试?在室内?
其他应用程序使用哪些提供商?网络提供商?
我的猜测是,正在运行的应用程序使用网络提供商,并且由于您获取的是最后一个已知的网络提供商,因此它将从之前的应用程序位置 ping 返回该位置。网络提供商通过提供大致位置来帮助分配 GPS 卫星。因此,当您启动应用程序(运行其他应用程序后)时,您拥有卫星,但是(因为您可能在室内)如果没有网络,您可能会逐渐丢失卫星,从而降低准确性。
这个故事的寓意是:也要使用网络提供商,或者确保您有清晰的天空视野来测试 GPS。
Where are you testing? Indoors?
What providers are the other apps using? Network Provider?
My guess is that the apps that are working use Network provider and since you are getting the last known, it will return that location from the previous app location ping. The Network provider helps allocate gps satellites by giving a general location. So when you start app (after running other apps), you have satellites, but (since you are probably indoors) without the network you may lose satellites little by little, decreasing accuracy.
moral of the story: Use the network provider as well, or make sure you have clear view of the sky to test gps.
好吧,弄清楚这是一个其他人也看到过的问题:显然(至少在某些手机上)如果相机处于活动状态,GPS 侦听器的工作效果会更差。
使用 CameraPreview 时获取 GPS 数据时出现问题
Android:丢失 GPS当我启动相机和 SurfaceView 时,某些手机上的信号强度
Android 中的相机预览和 GPS 读取
为此创建了 Andorid 问题 http://code.google.com/p/android/issues/detail?id=20098
如果有人有解决方案,请分享
Ok, figure out that this is an issue that was also seen by other people: apparently (at least on some phones) GPS listener works much worse if Camera is active.
Problem with getting GPS data when using CameraPreview
Android: Losing GPS signal strength on some phones when I start up the Camera and SurfaceView
Camera Preview and GPS reading in Android
Created Andorid issue for that http://code.google.com/p/android/issues/detail?id=20098
If someone has a solution pls share