App starts searching for GPS right on app start, not when needed
Asked Answered
G

3

6
  • I have an Android app that has Google Maps V2 as part of functionality.
  • I have

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

    in my manifest, and everything else needed for maps to work.

  • My app starts not on the screen with maps.

Now the question is why does my phone (Galaxy Nexus, just in case) starts showing GPS icon in status bar right when app starts, but not when I get to the screen with maps and start to work with it? I don't need to track my location and use battery power when I'm not on maps screen.

For example What's App messenger also uses GPS for its map but the icon is showed only when you open the map screen, not right on the first activity that is launched.

Googled for couple of hours but found nothing at all. Any help will be appreciated!

Edited:

MapActivity class

private LocationListener mLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
};

@Override
public void onCreate(Bundle savedState) {
    super.onCreate(savedState);
    setContentView(R.layout.map_activity);

    startGPS();
    initMap();
    mMapView = (MapView) findViewById(R.id.map_google_map);
    mMapView.onCreate(null);
    mGoogleMap = mMapView.getMap();
    if (mGoogleMap != null) {
        customizeGoogleMap();
        loadAndFillMap();
    }
}

private void startGPS() {
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
}

private void initMap() {
    try {
        MapsInitializer.initialize(this);
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }
}

private void customizeGoogleMap() {
    mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
    mGoogleMap.setMyLocationEnabled(true);
}

private void loadAndFillMap() {
    new LoadAndFillMapTask().execute();
}

private class LoadAndFillMapTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        String address = Session.appData().getSelectedAddress();
        mMapLocation = Api.getMapApi().getMapLocation(address);
        return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        fillMap();
    }
}

private void fillMap() {
    // filling Google Map with markers etc.
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mMapView != null) {
        mMapView.onDestroy();
    }
    mLocationManager.removeUpdates(mLocationListener);
}
Glimpse answered 4/6, 2013 at 7:25 Comment(2)
Can we see some code of the main activity please ?Insouciant
@Insouciant Main activity contains absolutely nothing related to google maps, i honestly see no point in showing what is going on there. Moreover the app starts with splash screen (at that point GPS icon is already displayed in status bar) which contains just an intent to main activity delayed for 1 second.Glimpse
G
11

After a while we found out that the problem was in Flurry SDK we were using in our project...

By default Flurry starts to report location right from app start. To turn it off we used:

FlurryAgent.setReportLocation(false);

... /_-

Glimpse answered 17/6, 2013 at 7:22 Comment(3)
Oh sweet Jesús, I would've never found out what was causing it if it wasn't for you!Lobbyism
I started deleting everything in my app and start re-enable bit-by-bit of code to find out that it was Flurry. Then with a search there came up your question and reply. I wish I had found it earlierMoschatel
Thanks for finding this, would have never guessed it was caused by flurry!Waring
L
0

You'll have to implement the successive method of location-fetching: Using GPS(only if it is on), then Wifi, and then the data-connection. Also, apart from FINE_LOCATION, use the following too--

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
Locksmith answered 4/6, 2013 at 7:30 Comment(3)
Can you please be more specific about "the successive method of location-fetching"? Also from documentation on ACCESS_MOCK_LOCATION you can see it is used for testing purposes.Glimpse
Try to follow the link-- developer.android.com/guide/topics/location/strategies.html If you need a detailed code for that, lemme know and i'll post it as another answer hereLocksmith
Thanks for your answer, Kunal. I've added some code of my map class above. I tried to use an approach described in the link you gave: see mLocationListener, startGPS method, onDestroy method. However on my Galaxy Nexus GPS still starts right from app start, not when I enter map activity.Glimpse
G
0

I'm not sure whether it is the best solution but I've used this approach.

In my starting activity I do like this:

@Override
public void onCreate(Bundle savedState) {
    super.onCreate(savedState);
    if (GpsUtils.canToggleGps(this)) {
        GpsUtils.turnGpsOff(this);
    }
    //...
}

And in my MapActivity:

@Override
public void onCreate(Bundle savedState) {
    super.onCreate(savedState);
    if (GpsUtils.canToggleGps(this)) {
        GpsUtils.turnGpsOn(this);
    }
    //...
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (GpsUtils.canToggleGps(this)) {
        GpsUtils.turnGpsOff(this);
    }
    //...
}

GPS icon in status bar doesn't bother me anymore.

Glimpse answered 11/6, 2013 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.