问题描述
我正在尝试使我的GPS在某项服务中正常工作,但无法正常工作. GPS位可以单独工作,但不能在服务中工作.
Im trying to get my GPS working in a service but its not working. The GPS bit works on its own but not in the service.
我尝试使用System.out.println()进行调试,喜欢它停止工作的地方,但无法弄清为什么一切看起来都很好.
I have tried debugging with System.out.println() and fond where it stops working but cant work out why it all looks good.
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("test 1");
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
System.out.println("test 2");
LocationListener lli = new myLocationListener();
System.out.println("test 3");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, lli);
System.out.println("test 4");
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
class myLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null){
pet.setMeter();
}
}
进入测试4,然后死亡.我很迷茫,为什么要有人帮助的话,那真是太感谢了.
It gets to Test 4 then dies. I am lost at why so if anyone can help that would be awesome thanks.
推荐答案
一个不错的gps跟踪器指南: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
A nice gps tracker guide: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
只需将this.mLocation =位置添加到onLocationChanged
Just needed to add this.mLocation = location in onLocationChanged
他还将其包装到服务中.
He also wraps it into a service.
这篇关于Android GPS无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!