Одно из моих приложений для Android должно получить позиции gps, если приложение находится в состоянии уничтожения. Мое приложение получает позиции gps в настоящее время, когда оно находится в running(active)
и pause(background)
режимах. Но когда я уничтожил свое приложение, служба работает, но LocationListener
onLocationChanged(Location location)
не звонит. Как я могу решать такие проблемы ?.Нужно получить позиции gps, если приложение убивает или уничтожает в android
public class LocationFinder extends Service {
public static double lat, lng;
LocationManager locationManager;
public void onDestroy() {
super.onDestroy();
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.v("location", "===>location ed onStartCommand " + lat + "==>"
+ lng);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
getLocation();
}
});
return START_STICKY;
}
final LocationListener locationListener = new LocationListener() {
// this method is not called when we destroyed the app. i want to call this method even app is in destoyed.
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
Toast.makeText(
getApplicationContext(),
"Location Finder : onLocation Changed "
+ location.getLatitude() + " "
+ location.getLongitude(), Toast.LENGTH_LONG)
.show();
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
public Location getLocation() {
Variables.writeLog("Enter into getLocation : \n", false);
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(context);
Location location = null;
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
Log.v("Location", "Before");
if (locationManager != null) {
String provider = locationManager.getBestProvider(criteria, true);
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (provider != null) {
if (isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 10, 0,
locationListener);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 10, 0,
locationListener);
if (location != null) {
updateWithNewLocation(location);
} else {
location = locationManager
.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider,
10, 0, locationListener);
}
} else {
location = locationManager
.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 10, 0,
locationListener);
}
} else {
location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 10, 0,
locationListener);
}
} else {
if (locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 10, 0,
locationListener);
} else if (locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 10, 0,
locationListener);
} else if (locationManager
.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {
locationManager.requestLocationUpdates(
LocationManager.PASSIVE_PROVIDER, 10, 0,
locationListener);
}
}
}
return location;
}
private void updateWithNewLocation(Location location) {
if (location != null) {
Log.v("location", "===>location ed " + lat);
lat = location.getLatitude();
lng = location.getLongitude();
Variables.writeLog("Lat :" + lat + " lng : " + lng, false);
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
Оставьте свой код. – Jas
@ Кристи вы решили свою проблему? – Tony
Где код? –