When app is killed service is stopped

I have created service that calls an API and gets a response. Based on the response we update status. When the app is in the foreground, the service works well but when the application is killed, the time service is stopped and doesn't get the API response.

What I want:

When the service starts and the API is called but the app is killed by the user before the response comes, the service should continue execution without restart and not be stopped. Please check following code.

   public class Send_Service extends Service {
   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {

       StringRequest request = new StringRequest(Request.Method.POST, url, new 
         Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                   // my code
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> param = new HashMap<String, String>();
                    param.putAll(params);
                    return param;
                }
            };
            request.setShouldCache(false);
            // Adding request to request queue
            request.setRetryPolicy(new DefaultRetryPolicy(
                    60000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            RequestQueue mRequestQueue = Volley.newRequestQueue(this);
            mRequestQueue.add(request);

        }


    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
    }
}
  return Service.START_NOT_STICKY;}

Manifest

 <service android:name=".services.Sendfax_Service"
        android:enabled="false"
        />
<!--calling service-->
 Intent i = new Intent(this, Sendfax_Service.class);
 startService(i);

Upvotes: 0

Views: 813

Answers (2)

Anton Bevza
Anton Bevza

Reputation: 463

You need to make your service foreground. https://developer.android.com/guide/components/services.html#Foreground

But for this case, better to use Job-Scheduling https://developer.android.com/topic/performance/scheduling.html

Upvotes: 0

Sonny Hansen
Sonny Hansen

Reputation: 630

You should go with

START_STICKY Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent.

Instead of

START_NOT_STICKY Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), and there are no new start intents to deliver to it, then take the service out of the started state and don't recreate until a future explicit call to Context.startService(Intent).

If you want it running even when the process is killed

Upvotes: 1

Related Questions