2017-05-29 4 views
-1

Ich entwickle eine Tracking-Anwendung in Android.Android: IntentService stoppt plötzlich

Während ich getestet, um die Anwendung ich ein Problem gefunden:

Ich verwende einen Alarmmanager WakefulBroadcast zu starten, die eine IntentService aufruft, in der OnHandleIntent Methode Ich rufe requestLocationUpdates() von FusedLocation, und Starten eines Handler/CountDownTimer to removeLocationUpdates

Das Problem ist, dass manchmal der Dienst plötzlich ohne Warnung oder Erklärung stoppt.

Und das Protokoll hält, welche die Locationarbeits oder die CountDownTimer ausgeführt

Kann mir jemand helfen, warum mein IntentService plötzlich gestoppt wird? Oder gibt es eine andere Möglichkeit, den Dienst während des Empfangs der locationUpdates am Laufen zu halten?

Servicemanager:

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

}

ExecSuscriptionService:

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

Listener:

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 Nächste anfrage in: 0 minutes 2017-05-29 15: 11: 14.000

18 Conectando a GoogleServices 2017.05.29 15.11.15 2017.05.29 15: 11: 14,000

18 Conectado ein GoogleServices 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 encendido y ejecutando 2017-05-29 15: 11: 14.000

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

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

18 Servicio 1496088675756 encendido y ejecutando 2017.05.29 15: 11: 34,000

18 Ejecutando ExecSubscriptionService 2017-05-29 15:26:17 2017-05-29 15:26: Diese 16,000


Antwort

2

ist keine geeignete Verwendung von IntentService. Sobald onHandleIntent() endet, ist Ihr IntentService zerstört. Ihr Prozess kann zu jedem späteren Zeitpunkt beendet werden.

Ersetzen Sie die IntentService durch eine regelmäßige Service, Ihre Arbeit in onStartCommand(). Rufen Sie stopSelf(), wenn Sie mit dem Service fertig sind.

+0

Dies funktionierte wie ein Charme. Ich danke dir sehr. Ich dachte, dass der Handler den Trick machen würde, den "One Shot" zu behalten, ich änderte meinen IntentService in einen Service und es funktionierte perfekt: D –