Android GPS not working in service

[亡魂溺海] 提交于 2019-12-11 19:39:51

问题


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.

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();


        }

    }

It gets to Test 4 then dies. I am lost at why so if anyone can help that would be awesome thanks.


回答1:


A nice gps tracker guide: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

Just needed to add this.mLocation = location in onLocationChanged

He also wraps it into a service.



来源:https://stackoverflow.com/questions/19062104/android-gps-not-working-in-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!