设备锁定android时GPS不更新位置

时间:2015-07-20 13:59:08

标签: android gps android-location fusedlocationproviderapi

我需要创建实时路径跟踪应用。我使用FusedLocationProviderApi 来获取当前位置。我的问题是当设备被锁定时FusedLocationProvider如何更新位置。当跟踪正在进行时,我尝试使用WAKE_LOCK进行设备唤醒,但这会导致设备停止并导致电池耗尽。请建议我摆脱它。以下是我的位置服务代码

  public class LocationService extends Service {
public static float MIN_DIST = 50.0f;
public static float MIN_SPEED = 0.0f;

public static final String BROADCAST_ACTION = "LocationService.LOCATION_UPDATE";

public LocationManager locationManager;
public MyLocationListener listener;

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 5*1000; // in mili


Intent intent;
String tag=getClass().getSimpleName();

PowerManager powerManager;
 // WakeLock wakeLock;

Timer checkLastLocUpdateTimer; // timer to check time of last loc update and reset loc update if required

@Override
public void onCreate() {
    super.onCreate();
    intent = new Intent(BROADCAST_ACTION);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    listener = new MyLocationListener();

    powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    /*wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "FUSED_LOCATOIN_SERVICE_WAKE_LOCK");*/
    checkLastLocUpdateTimer = new Timer();
    //Set the schedule function and rate
    checkLastLocUpdateTimer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() 
        {

            long maxWaitTimeforUpdate = 60*1000; // in millis
            long currentTime = System.currentTimeMillis();
            long lastLocUpdateTime = Pref.getLastLocUpdateTime(getApplicationContext());
            long timeDiff = currentTime- lastLocUpdateTime;
            Log.e(tag,"Time since last loc update: "+timeDiff);

        if(timeDiff>maxWaitTimeforUpdate)
            {
                Log.e(tag,"Time since last loc update is more than max waiting time: "+timeDiff);

                //if(mGoogleApiClient.isConnected())
                {
                    Log.e(tag,"Restarting Location Updates..." );// 


                    startStopLocationUpdateHandler.sendEmptyMessage(0);
                }
            }

        }

    },
    //Set how long before to start calling the TimerTask (in milliseconds)
    30*1000,
    //Set the amount of time between each execution (in milliseconds)
    30*1000);


    Log.e(tag,"OnCreate()");
}

private final Handler startStopLocationUpdateHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        stopLocationUpdates();
        startLocationUpdates();
    }
};  

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e("LocationService", "onStartCommand");
  //        wakeLock.acquire();
    startLocationUpdates();

    return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    super.onDestroy();
    checkLastLocUpdateTimer.cancel();
    stopLocationUpdates();
    /*if(wakeLock.isHeld())
    {
        wakeLock.release();
    }*/
    Log.e(tag,"OnDestroy()");

}

protected void startLocationUpdates()
{
    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            MINIMUM_TIME_BETWEEN_UPDATES,
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
            listener);
    Log.e(tag,"Requested location update");

}

protected void stopLocationUpdates()
{
    locationManager.removeUpdates(listener);
    Log.e(tag,"Location updates removed");

}

public class MyLocationListener implements LocationListener {

    public void onLocationChanged(final Location loc) 
    {
        Log.e(tag, "locationChanged "+loc.toString());


        Pref.setLastLocUpdateTime(System.currentTimeMillis(), getApplicationContext());

        intent.putExtra("Latitude", loc.getLatitude());
        intent.putExtra("Longitude", loc.getLongitude());
        intent.putExtra("Provider", loc.getProvider());
        intent.putExtra("Accuracy", loc.getAccuracy());
        intent.putExtra("Altitude", loc.getAltitude());
        intent.putExtra("Bearing", loc.getBearing());
        intent.putExtra("Speed", loc.getSpeed());
        intent.putExtra("Time", loc.getTime());
        sendBroadcast(intent);

    }

    public void onProviderDisabled(String provider) {

        Log.e(tag,"onProviderDisabled: "+provider.toString());
    }

    public void onProviderEnabled(String provider) {

        Log.e(tag,"onProviderEnabled: "+provider.toString());
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {

        Log.e(tag,"onStatusChanged- provider: "+provider.toString()+" status: "+status);
    }
}

}

0 个答案:

没有答案