安卓后台定位服务不断消亡



此服务的目的是在后台更新用户的当前位置,我不想要前台服务。然后每隔 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多个小时的应用程序。

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

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

创建前台服务是拥有长时间运行的服务的最佳选择

最新更新