I am working on an application in which I have to hit the RESTful web service in every 5 min to check whether the new data is updated or not even if the application is closed by the user. I did this
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "http://www.abcert.com/wp-json/wp/v2/posts";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
boolean Str;
try {
JSONArray jsonArray =new JSONArray(response);
Log.i("JSON",""+jsonArray);
JSONObject jsonObject = jsonArray.getJSONObject(0);
int id = jsonObject.getInt("id");
Log.i("MyService is on the",""+id);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Hey","Something went wrong");
}
});
//add request to queue
requestQueue.add(stringRequest);
return START_STICKY;
}
But Didn't get success. START_STICKY is not working when I close the app service is also killed.