cherry
cherry

Reputation: 77

Gps tracker icon stays alive even thow the acitvity is killed i need to close my application to kill it. In android

import android.location.GpsStatus;
import android.location.GpsStatus.Listener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;


@SuppressLint("NewApi")
public class SpaLocationActivity extends Activity implements LocationListener {
    protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
double latitude;
protected double longitude; 
protected boolean gps_enabled,network_enabled;
Intent mapIntent;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spa_location);
txtLat = (TextView) findViewById(R.id.textView1);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);

Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
txtLat = (TextView) findViewById(R.id.textView1);
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
latitude=location.getLatitude();
longitude=location.getLongitude();
Uri locationUri = Uri.parse("geo:"+latitude+","+longitude+"?z=14");
 mapIntent = new Intent(Intent.ACTION_VIEW, locationUri);

}
@Override
public void onLocationChanged(Location location) {

//txtLat = (TextView) findViewById(R.id.textView1);
//txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());


}

@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    locationManager.removeUpdates(locationListener);
//  locationManager.removeGpsStatusListener((Listener) locationListener);
//  locationManager.removeNmeaListener(listener)

}

public void openMap(View view){
    startActivity(mapIntent);
}
}

**
//Heres my code look into it once and help me out...I have seen many blogs but dint find a right appt for my code .i have seen many posts were it is possible by using mapview and MyLocationOverlay but i am just using implect intent and calling the default map on the phone. I am using the default maps in phone and not the map view

**

Upvotes: 0

Views: 263

Answers (2)

cherry
cherry

Reputation: 77

i found the solution .. in onPause() method i have used a simple method and its gone.

protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
// as i am implementing the class location listner i used this in parmeters
    locationManager.removeUpdates(this);
}

Upvotes: 1

Havtaske
Havtaske

Reputation: 21

Just stop the GPS in your onPause:

@Override
protected void onPause() {
    super.onPause();
    // Stop GPS
    lClient.disconnect();
}

And start it in your onResume

Upvotes: 0

Related Questions