Android 安卓:IntentService突然停止

Android 安卓:IntentService突然停止,android,intentservice,fusedlocationproviderapi,Android,Intentservice,Fusedlocationproviderapi,我正在用android开发一个跟踪应用程序 在测试应用程序时,我发现了一个问题: 我使用AlarmManager启动WakefulBroadcast调用IntentService,在OnHandleIntent方法中,我从FusedLocation调用requestLocationUpdates(),并启动一个处理程序/倒计时程序来重新设置更新 问题是,有时服务突然停止,没有警告或解释 并且日志停止显示locationUpdate工作或执行的倒计时 有人能帮我知道为什么我的IntentServi

我正在用android开发一个跟踪应用程序

在测试应用程序时,我发现了一个问题:

我使用AlarmManager启动WakefulBroadcast调用IntentService,在OnHandleIntent方法中,我从FusedLocation调用requestLocationUpdates(),并启动一个处理程序/倒计时程序来重新设置更新

问题是,有时服务突然停止,没有警告或解释

并且日志停止显示locationUpdate工作或执行的倒计时

有人能帮我知道为什么我的IntentService突然停止吗?或者,在接收LocationUpdate时,是否有其他方法保持服务的执行/活动状态

服务经理:

public void StartExecuteSubscriptionService(long nextTimeMillis){
    Intent intent = new Intent(contextWeakReference.get(),ExecSubscriptionWakefulBroadcast.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(contextWeakReference.get(),
            Constants.EXEC_SUBSCRIPTIONS_SERVICE_REQUESTCODE,
            intent, 0);


    final int SDK_INT = Build.VERSION.SDK_INT;
    AlarmManager am = (AlarmManager) contextWeakReference.get().getSystemService(Context.ALARM_SERVICE);

    if (SDK_INT < Build.VERSION_CODES.KITKAT) {
        am.set(AlarmManager.RTC_WAKEUP, nextTimeInmillis, pendingIntent);
    }
    else if (Build.VERSION_CODES.KITKAT <= SDK_INT  && SDK_INT < Build.VERSION_CODES.M) {
        am.setExact(AlarmManager.RTC_WAKEUP, nextTimeInmillis, pendingIntent);
    }
    else if (SDK_INT >= Build.VERSION_CODES.M) {
        am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextTimeInmillis, pendingIntent);
    }

}
}

ExecScript服务:

@Override
protected void onHandleIntent(@Nullable Intent intent) {

    CatchExceptionsUtils.saveException(this,new Exception("Ejecutando ExecSubscriptionService "+TelephonyUtils.getSystemDate("yyyy-MM-dd HH:mm:ss")));

        try{
            LocationUpdateRequester locationUpdateRequester = new LocationUpdateRequester(this.getApplicationContext(),intent);
            locationUpdateRequester.requestLocationUpdates();
        }catch (Exception e){
            CatchExceptionsUtils.saveException(this,e);
        }
}
LocationUpdateRequester:

public void requestLocationUpdates() throws SecurityException {
    try{
        googleApiClient.connect();
    }catch (Exception e){
        CatchExceptionsUtils.saveException(contextWeakReference.get(),e);
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) throws SecurityException {
    try{
        CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Conectado a GoogleServices "+TelephonyUtils.getSystemDate("yyyy-MM-dd HH:mm:ss")));


        CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Iniciando la busqueda "+TelephonyUtils.getSystemDate("yyyy-MM-dd HH:mm:ss")));
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, TelephonyUtils.createLocationRequest(),listener);

        countDownTimer = new CountDownTimer(Constants.TIMEOUT_GPS,20000) {
            @Override
            public void onTick(long millisUntilFinished) { CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Servicio "+ejecucionId+" encendido y ejecutando"));
            }

            @Override
            public void onFinish() {
                CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Ejecutando CountDown "+TelephonyUtils.getSystemDate("yyyy-MM-dd HH:mm:ss")));
                LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,listener);
                googleApiClient.disconnect();
                releaseWake();

            }
        }.start();           

    }catch (Exception e){
        CatchExceptionsUtils.saveException(contextWeakReference.get(),e);
        releaseWake();
    }
}
听众:

LocationListener listener = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        if(location!=null){
            CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Guardando registro; " +location.getAccuracy()+" "+location.getLatitude()+" "+location.getLongitude()+" "+TelephonyUtils.getSystemDate("yyyy/MM/dd HH:mm:ss")+" "+ejecucionId));

            locationList.add(location);
        }
    }
};

18 Ejecutando ExecutionSubscriptionWakefulBroadcast 2017-05-29 15:11:14 2017-05-29 15:11:13.000

18 Ejecutando执行订阅服务2017-05-29 15:11:15 2017-05-29 15:11:13.000

18.新闻发布会时间:0分钟2017-05-29 15:11:14.000

18康康坦多a谷歌服务2017-05-29 15:11:15 2017-05-29 15:11:14.000

18 Conectado a GoogleServices 2017-05-29 15:11:15 2017-05-29 15:11:14.000

18伊尼西亚多拉布斯奎达2017-05-29 15:11:16 2017-05-29 15:11:14.000

18 Busqueda iniciada 2017-05-29 15:11:16 2017-05-29 15:11:14.000

18 Servicio 1496088675756 encendido y ejecutando 2017-05-29 15:11:14.000

18 Guardando registro;2017/05/29 15:11:17 1496088675756 2017-05-29 15:11:15.000

18 Guardando registro;2017/05/29 15:11:32 1496088675756 2017-05-29 15:11:31.000

18 Servicio 1496088675756 encendido y ejecutando2017-05-29 15:11:34.000

18 Ejecutando执行订阅服务2017-05-29 15:26:172017-05-29 15:26:16.000



这不适合使用
IntentService
。一旦
onHandleIntent()
结束,您的
IntentService
将被销毁。您的进程可以在此后的任何时候终止


IntentService
替换为常规的
服务
,在
onStartCommand()
中开始工作。完成服务后,请调用
stopSelf()

这很有效。非常感谢你。我以为处理程序会保持“一次性”的技巧,我将我的IntentService更改为一个服务,它工作得非常完美:D
LocationListener listener = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        if(location!=null){
            CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Guardando registro; " +location.getAccuracy()+" "+location.getLatitude()+" "+location.getLongitude()+" "+TelephonyUtils.getSystemDate("yyyy/MM/dd HH:mm:ss")+" "+ejecucionId));

            locationList.add(location);
        }
    }
};