开发者

why is my gps getting "hung up?"

开发者 https://www.devze.com 2023-04-11 21:45 出处:网络
I am trying to implement a gps location listener that will stay on and continuously update in the background. I realize this is bad practice but开发者_Go百科 I am doing this for testing purposes. Righ

I am trying to implement a gps location listener that will stay on and continuously update in the background. I realize this is bad practice but开发者_Go百科 I am doing this for testing purposes. Right now I have a location listener that gets called with getGPS(). It can also be killed with killGPS().

It works perfectly when im walking around and I click my get gps button which takes the latitude and longitude from my location parameter which I get from

    GPSActivity.getGPS();
    loc_listener = GPSActivity.loc_listener;
     locationManager = (LocationManager)
     getSystemService(LOCATION_SERVICE);
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
     0, 0, loc_listener);
    location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

However in the background, sometimes it will say I'm still at my apartment even though I've been out for 3hours! Why is my location listener not updating itself even though it turns on to find a new location every 10 mins?

Thanks

package com.cellphone;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GPSActivity extends Activity{
    public static LocationListener loc_listener = null;

    public static void getGPS() {
        if (loc_listener == null) {
            loc_listener = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub

                }
            };
        }

    }

    public static void killGPS(LocationManager locationManager) {
        if (locationManager != null && loc_listener != null) {
            locationManager.removeUpdates(loc_listener);
        }
    }

}


I would recommend only using the Activity to display information and use a service in the background that holds a partial wake lock so it continues to run even if the device tries to go into sleep mode. Then use a broadcastreceiver in the Activity to get updates from the service with the location.

Regardless of how you manage it you need to hold a wake lock to ensure it continues running.

Check this Link:
http://developer.android.com/reference/android/os/PowerManager.html

You must hold permission:

<uses-permission android:name="android.permission.WAKE_LOCK" />

And for the wake lock:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");

//you would acquire in onStart() or on your getGPS() or somewhere else if you like.
mWakeLock.acquire();
// you would release in your onStop() or your onDestroy() or on your killGPS()
mWakeLock.release();


for location poller, here is simple & working example: https://github.com/commonsguy/cwac-locpoll

you can keep state of your app by adding a function in Application context. So to say, location poller can be aware of app-state and skip gps action if app is not active.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号