Search code examples
androidokhttprobospice

Robospice service running until it finish it job after application finished


I have an application that access to a server. When I quit the application, I have to disconnect from the server first, then close the application.

I would like to know if it's possible (and how) to make a Robospice service (background task) that disconnect from the server even if the application is closed (and the Robospice service is still running to finish the deconnection, and then auto kill itself after).

The problem is that the deconnection is too long (sometimes more than 5 secondes) and I would like to avoid blocking the phone during the deconnection, and allow the user to use it's phone.

Another question : is the Robospice librairy will be maintained and improved in the future (if necessary) ?


Solution

  • I've solved my problem (that may be wasn't really a problem !) by ending my application with finish()

    In my MainActivity, I use a Robospice service :

    private final class LogoutListener implements PendingRequestListener<String> {
    
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Log.e(TAG, spiceException.getMessage());            
        }
    
        @Override
        public void onRequestSuccess(String result) {
            // nothing special
        }
    
        @Override
        public void onRequestNotFound() {
            Log.w(TAG, "Not found");
        }
    }
    
    private void Alarm_Logout(boolean exit) {
        Logout request = new Logout(url);
        spiceManager.execute(request, new LogoutListener());
        this.finish();
    }
    

    And the Lougout class :

    public class Logout extends OkHttpSpiceRequest<String> {
    
      private String url_logout;
    
      public Logout(String url) {
          super(String.class);
          this.url_logout = url;
      }
    
      @Override
      public String loadDataFromNetwork() throws Exception {
    
          OkHttpClient client = new OkHttpClient();
          Request request = new Request.Builder()
                  .url(url_logout)
                  .build();
          Response response = client.newCall(request).execute();
          return "ok";
    
      }
    
    }
    

    Before I closed the app with System.exit(0) in onRequestSuccess, so I had to wait the Logout complete. Now the app close (with finish()), but the Logout continue in background, and then, when done, the service finish...