【问题标题】:Android: IntentService suddenly stopsAndroid:IntentService 突然停止
【发布时间】:2017-05-29 21:44:34
【问题描述】:

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

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

我正在使用 AlarmManager 启动一个调用 IntentService 的 WakefulBroadcast,在 OnHandleIntent 方法中我从 FusedLocation 调用 requestLocationUpdates(),并启动一个 Handler/CountDownTimer 来删除LocationUpdates

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

并且日志停止显示 locationUpdate 工作或 CountDownTimer 执行

有人可以帮我知道为什么我的 IntentService 突然停止了吗?还是有其他方法可以在接收 locationUpdates 时保持服务执行/活动?

服务管理器:

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);
    }

}

WakefulBroadcast:

public class ExecSubscriptionWakefulBroadcast extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, ExecSubscriptionsService.class);
    // Start the service, keeping the device awake while it is launching.
    CatchExceptionsUtils.saveException(context,new Exception("Ejecutando ExecSubscriptionWakefulBroadcast "+ TelephonyUtils.getSystemDate("yyyy-MM-dd HH:mm:ss")));
    startWakefulService(context, service);
}

}

执行订阅服务:

@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 ExecSubscriptionWakefulBroadcast 2017-05-29 15:11:14 2017-05-29 15:11:13.000

18 Ejecutando ExecSubscriptionService 2017-05-29 15:11:15 2017-05-29 15:11:13.000

18 Próxima suscripcion inicia en: 0 minutos 2017-05-29 15:11:14.000

18 连接 Google 服务 2017-05-29 15:11:15 2017-05-29 15:11:14.000

18 连接 Google 服务 2017-05-29 15:11:15 2017-05-29 15:11:14.000

18 Iniciando la busqueda 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 enendido y ejecutando 2017-05-29 15:11:14.000

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

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

18 Servicio 1496088675756 enendido y ejecutando 2017-05-29 15:11:34.000

18 Ejecutando ExecSubscriptionService 2017-05-29 15:26:17 2017-05-29 15:26:16.000


【问题讨论】:

    标签: android intentservice fusedlocationproviderapi


    【解决方案1】:

    这不适合使用IntentService。一旦onHandleIntent() 结束,您的IntentService 就会被销毁。此后您的进程可以随时终止。

    IntentService 替换为常规Service,在onStartCommand() 中开始您的工作。服务完成后致电stopSelf()

    【讨论】:

    • 这就像一个魅力。太感谢了。我认为 Handler 会做到保持“一枪”,我将 IntentService 更改为 Service 并且它运行良好:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2021-02-01
    • 2020-11-05
    • 2020-10-09
    • 2016-12-29
    • 2018-05-09
    相关资源
    最近更新 更多