Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I got the same problem and I tried to solve it with the good answer of Devunwired, but I had some troubles. I couldn't find a way to stop the service and when I finished my app the GPS-module was still running. So i found another way:</p> <p>I wrote a GPS.java class:</p> <pre><code>public class GPS { private IGPSActivity main; // Helper for GPS-Position private LocationListener mlocListener; private LocationManager mlocManager; private boolean isRunning; public GPS(IGPSActivity main) { this.main = main; // GPS Position mlocManager = (LocationManager) ((Activity) this.main).getSystemService(Context.LOCATION_SERVICE); mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); // GPS Position END this.isRunning = true; } public void stopGPS() { if(isRunning) { mlocManager.removeUpdates(mlocListener); this.isRunning = false; } } public void resumeGPS() { mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); this.isRunning = true; } public boolean isRunning() { return this.isRunning; } public class MyLocationListener implements LocationListener { private final String TAG = MyLocationListener.class.getSimpleName(); @Override public void onLocationChanged(Location loc) { GPS.this.main.locationChanged(loc.getLongitude(), loc.getLatitude()); } @Override public void onProviderDisabled(String provider) { GPS.this.main.displayGPSSettingsDialog(); } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } } } </code></pre> <p>This class is used in every Activity which needs the GPS coordinates. Every Activity has to implement the following Interface (needed for the communication): </p> <pre><code>public interface IGPSActivity { public void locationChanged(double longitude, double latitude); public void displayGPSSettingsDialog(); } </code></pre> <p>Now my main Activity looks like that:</p> <pre><code>public class MainActivity extends Activity implements IGPSActivity { private GPS gps; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gps = new GPS(this); } @Override protected void onResume() { if(!gps.isRunning()) gps.resumeGPS(); super.onResume(); } @Override protected void onStop() { gps.stopGPS(); super.onStop(); } public void locationChanged(double longitude, double latitude) { Log.d(TAG, "Main-Longitude: " + longitude); Log.d(TAG, "Main-Latitude: " + latitude); } @Override public void displayGPSSettingsDialog() { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } } </code></pre> <p>and a second one like that:</p> <pre><code>public class TEST4GPS extends Activity implements IGPSActivity{ private GPS gps; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.gps = new GPS(this); } @Override public void locationChanged(double longitude, double latitude) { Log.d("TEST", "Test-Longitude: " + longitude); Log.d("TEST", "Test-Latitude: " + latitude); } @Override protected void onResume() { if(!gps.isRunning()) gps.resumeGPS(); super. onResume(); } @Override protected void onStop() { gps.stopGPS(); super.onStop(); } @Override public void displayGPSSettingsDialog() { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } } </code></pre> <p>It's not as beautiful as the solution of Devunwired, but it works for me. cya</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload