0
這項服務的目的是在後臺更新用戶的當前位置,我不想要前臺服務。然後每50個位置寫一個GMX文件。Android背景定位服務一直奄奄一息
我面臨的問題是,在隨機數量的更新(並且通常在生成50個位置點之前)之後,服務因沒有明確的原因而死亡,然後服務再次啓動。 onDestroy在任何時候都不會被調用。
這裏的服務代碼(與startService發佈):
public class LocationService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private static LocationManager locationManager;
private final class ServiceHandler extends android.os.Handler{
private LocationManager locationManager;
public ServiceHandler(Looper looper){
super(looper);
}
@Override
public void handleMessage(Message msg) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
run(locationManager);
}
}
@Override
public void onCreate() {
HandlerThread thread = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
Log.i("Service","onHandleIntent lancé");
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
Log.i("Service","onHandleIntent lancé");
return START_STICKY; //permet de redémarrer le service s'il est tué
}
@Override
public void onDestroy() {
Log.i("DESTROY","IsDestroyed");
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected void run(LocationManager locationManager){
Log.i("Service","Service lancé");
boolean hasLocationFeature = checkLocationFeature();
if (hasLocationFeature && (ACCESS_FINE_LOCATION == PackageManager.PERMISSION_GRANTED || ACCESS_COARSE_LOCATION == PackageManager.PERMISSION_GRANTED)) {
sLocation(locationManager);
}
}
protected void sLocation(LocationManager locationManagerArg){
final List<Location> locations = new ArrayList<>();
final LocationManager locationManager = locationManagerArg;
Log.i("sLocation","lancé");
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
saveNewLocation(location);
if (location != null) {
locations.add(location);
Log.i("Locationsize", String.valueOf(locations.size()));
if(locations.size()%50 == 0) {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + System.currentTimeMillis() + ".gpx");
GPXWriter(file, "test1", locations);
locations.clear();
}
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,0,locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5000,0,locationListener);
}
的方法寫GPX文件沒有問題。我已經在一個仿真器和兩個實際設備上測試了這一點,發生同樣的問題。它可能與記憶壓力無關。我的手機上已經運行了超過100小時的其他應用程序的其他LocationService。
爲什麼這項服務會死亡,我該怎麼辦? 感謝
你的最佳選擇,即使我只有4個過程和10左右運行的服務,我有比1進入或RAM更多可用?似乎很奇怪...... – Val
如果一個應用因爲任何原因使用了大量內存,那麼您可能有1個進程正在運行,而另一個應用的1個服務仍然可能會被終止。它並不奇怪,這是如何機器人和服務的工作 – tyczj
也許你應該讀服務'處理生命週期'再次https://developer.android.com/reference/android/app/Service.html – tyczj