简体   繁体   中英

How to run a service for indefinite period of time in android

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.

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.

Running an indefinite service is not the ideal solution for it. Read this .

Use a background scheduler for this purpose, as it also takes care of efficient usage of the battery.

Many Apps use Service which is connected to their DB, for their specific tasks and Run in the background.

Like Facebook runs MessagingService , Instagram runs NotificationService . If you want you can use handler same time you can also choose Scheduler .

Example using Handler :

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final Handler handler = new Handler();

handler.postDelayed(new Runnable() {
       public void run() {

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

     handler.postDelayed(this, 1000 * 60 * 5); //Code runs every 5 minutes
   }
  }, 0); //Initial interval of 0 sec

return START_STICKY;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM