Я использую этот сервис для получения широты и долготы, но когда я закрываю приложение, я не могу получить обновленную широту и долготу.
вот мой манифест
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kanwarpreet.location_manager">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activity_Location_Tracker">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".utils.MyService"
android:process=":my_service"
android:enabled="true" />
</application>
</manifest>
Класс обслуживания
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "BOOMBOOMTESTGPS";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;
public LocationListener(String provider) {
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
Log.e("Location Changed", "Location Changed");
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER),
new LocationListener(LocationManager.PASSIVE_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.PASSIVE_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]
);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}
Регистрация кошек при запуске приложения
05-22 12:35:53.754 10314-10322/? E/com.example.kanwarpreet.location_manager:my_service: Failed to send DDMS packet REAQ to debugger (-1 of 20): Broken pipe
05-22 12:35:54.048 10314-10314/? E/BOOMBOOMTESTGPS: LocationListener gps
05-22 12:35:54.048 10314-10314/? E/Location Changed: Location Changed
05-22 12:35:54.048 10314-10314/? E/BOOMBOOMTESTGPS: LocationListener network
05-22 12:35:54.048 10314-10314/? E/Location Changed: Location Changed
05-22 12:35:54.048 10314-10314/? E/BOOMBOOMTESTGPS: LocationListener passive
05-22 12:35:54.048 10314-10314/? E/Location Changed: Location Changed
05-22 12:35:54.048 10314-10314/? E/BOOMBOOMTESTGPS: onCreate
initializeLocationManager
05-22 12:35:54.055 10314-10314/? E/BOOMBOOMTESTGPS: onStartCommand
05-22 12:35:54.056 10314-10314/? E/BOOMBOOMTESTGPS: onProviderDisabled: network
05-22 12:35:54.057 10314-10314/? E/BOOMBOOMTESTGPS: onLocationChanged: Location[gps 33.000000,-127.000000 hAcc=20 et=+1h58m2s979ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=96]}]
05-22 12:35:54.058 10314-10314/? E/BOOMBOOMTESTGPS: onStatusChanged: gps
05-22 12:35:54.059 10314-10314/? E/BOOMBOOMTESTGPS: onLocationChanged: Location[gps 33.000000,-127.000000 hAcc=20 et=+1h58m2s979ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=96]}]
05-22 12:35:54.564 10314-10314/? E/BOOMBOOMTESTGPS: onStatusChanged: gps
Logcat, когда я убиваю свое приложение
05-22 12:37:03.069 10343-10343/com.example.kanwarpreet.location_manager:my_service E/BOOMBOOMTESTGPS: LocationListener gps
05-22 12:37:03.069 10343-10343/com.example.kanwarpreet.location_manager:my_service E/Location Changed: Location Changed
05-22 12:37:03.069 10343-10343/com.example.kanwarpreet.location_manager:my_service E/BOOMBOOMTESTGPS: LocationListener network
05-22 12:37:03.069 10343-10343/com.example.kanwarpreet.location_manager:my_service E/Location Changed: Location Changed
05-22 12:37:03.069 10343-10343/com.example.kanwarpreet.location_manager:my_service E/BOOMBOOMTESTGPS: LocationListener passive
05-22 12:37:03.069 10343-10343/com.example.kanwarpreet.location_manager:my_service E/Location Changed: Location Changed
05-22 12:37:03.069 10343-10343/com.example.kanwarpreet.location_manager:my_service E/BOOMBOOMTESTGPS: onCreate
initializeLocationManager
05-22 12:37:03.072 10343-10343/com.example.kanwarpreet.location_manager:my_service E/BOOMBOOMTESTGPS: onDestroy
05-22 12:37:03.073 10343-10343/com.example.kanwarpreet.location_manager:my_service E/BOOMBOOMTESTGPS: onProviderDisabled: network