The application starts searching for GPS right when the application starts, and not if necessary.

  • I have an Android app that has Google Maps V2 as part of the 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 the cards to work.

  • My application does not start on the map screen.

Now the question is why my phone (Galaxy Nexus, just in case) starts showing the GPS icon in the status bar immediately when the application starts, but not when I get to the map screen and start working with it? I do not need to track my location and use battery power when I'm not on the map screen.

For example, the Messenger application also uses GPS for its map, but the icon appears only when the map screen is opened, and not at the beginning of the first launching action.

Googled for several hours, but found nothing. Any help would be appreciated!

Edited by:

Class MapActivity

 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); } 
+6
source share
3 answers

After a while, we found out that the problem was in the Flurry SDK, which we used in our project ...

By default, Flurry starts reporting the location right from the start of the application. To disable it, we used:

 FlurryAgent.setReportLocation(false); 

... / _-

+11
source

You will need to implement a consistent location method: use GPS (only if it is turned on), then Wifi, and then connect to the data. In addition to FINE_LOCATION, also use the following:

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> 
0
source

I'm not sure if this is the best solution, but I used this approach .

In my initial activity, I 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); } //... } 

The GPS icon in the status bar no longer bothers me.

0
source

Source: https://habr.com/ru/post/946475/


All Articles