Turn off GPS icon when LocationListener is sleeping

橙三吉。 提交于 2019-12-03 13:49:00

问题


I am struggling a bit with the LocationListener in Android. I want to make an app that will get the current GPS location, and then afterwards sleep for a long time. A day or more. In this period of time i want the GPS notification icon to not show.

What i have now, is in the onLocationChanged a Thread.sleep(x) but this will keep the icon on in the sleep period. How can i do this, and is there a better approach than to use Thread.sleep?

Thanks in advance


回答1:


You have to turn off the LocationManager completly for that. I did it in my App, where I only check the Location every 10 seconds, 'cause I found out, that the turning off saves a bit more battery power than a min_distance or min_time of the LocationManager.

Something like:

// Turning off
mLocationManager.removeUpdates(gpsListener);
mLocationManager = null;

// Turning on again
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);            
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_MINTIME, GPS_MINDISTANCE, gpsListener);

The icon will disappear till the LocationManager is turned on again.




回答2:


If you have overlays, think to disable the location on them too :

@Override
protected void onResume()
{
    Log.i("ProjetTEA", "onResumeMain");
    if (mLocationListener != null)
    {
        mLocationOverlay.enableMyLocation();
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(mBestProvider, 10000, 2, mLocationListener);
    }
    super.onResume();
}

@Override
protected void onPause()
{
    Log.i("ProjetTEA", "onPauseMain");
    mLocationOverlay.disableMyLocation();
    mLocationManager.removeUpdates(mLocationListener);
    mLocationManager = null;
    super.onPause();
}


来源:https://stackoverflow.com/questions/3333237/turn-off-gps-icon-when-locationlistener-is-sleeping

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