【发布时间】:2011-11-16 09:06:31
【问题描述】:
下面是一个假设监听 GPS 更新并向主要活动发送通知的代码
但它似乎不起作用:)
没有错误,只有 onLocationChanged 没有被调用!
有趣的是:当我运行一些其他应用程序来获取 GPS 修复,然后启动我的应用程序 => onLocationChanged 被调用时,只有准确性会降低,最终修复会丢失。
我还尝试将 GPS 侦听器添加到此代码中=> 它可以工作,每秒都会报告视野中的卫星,永远不会获得修复。
这到底是怎么回事? :)
公共类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;
}
}
【问题讨论】:
-
你在哪里测试?室内?
-
不是。正如我所说:“其他应用程序能够得到修复”。当我在调用 onLocationChanged 之后切换回我的应用程序时,只有准确性会降低。而修复最终会丢失