Android后台位置服务不断死亡

时间:2017-07-06 14:34:50

标签: android service location

此服务的目的是在后台更新用户的当前位置,我不想要前台服务。然后每50个位置写一个GMX文件。

我面临的问题是服务在随机数量的更新(通常在生成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。

为什么这项服务会死,我该怎么办呢? 感谢

1 个答案:

答案 0 :(得分:0)

由于内存限制,操作系统可以随时终止您的服务,需要更多资源,设备上的电池保存配置文件,甚至现在在Android O中,当您的应用仅在后台运行时,操作系统终止将终止您的服务几分钟。你无能为力&#34;保证&#34;你的服务永远存在。

创建前台服务是拥有长期服务的最佳选择