I created a class called HomeService that extends the android.app.Service. I then declared and implemented a class called BackgroundService which extends Thread. In the run method of the class I call the member method run of Runnable which calls my sendRequest method which is responsible for the Volley string request.

My service is supposed update the HomeActivity Text view so I implemented a broadcast listener in the activity and in the sendRequest I send a broadcast with an intent filter and the response from the server.

I am hoping someone can just take a look at me approach illustrated in the code bellow and tell me if it is the best approach. I have a feeling it is a little overkill. Thanks

public class HomeService extends Service {
BackgroundService service;
Intent responseComplete;

public IBinder onBind(Intent intent) {
    return null;

public int onStartCommand(Intent intent, int flags, int startId) {

    if (!service.isRunning()) {
        service.isRunning = true;
    return super.onStartCommand(intent, flags, startId);

public void onCreate() {
    service = new BackgroundService();
    Log.d("CREATED>>>>", "Created");

public void onDestroy() {
    if (service.isRunning) {
        service.isRunning = false;
        service = null;

class BackgroundService extends Thread {
    public boolean isRunning = false;
    public long milliSecs = 10000;
    //        Handler networkRequest= new Handler();
    Runnable runTask = new Runnable() {
        public void run() {
            isRunning = true;
            while (isRunning) {
                try {
                    Log.d("HANDLER_RUNNABLE>>>>", "HANDLER");
                } catch (InterruptedException e) {
                    isRunning = false;

    public boolean isRunning() {
        return isRunning;

    public void run() {

    private void sendRequest() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.URL_HOME,
                new Response.Listener<String>() {
                    public void onResponse(String response) {
                        Log.d("RESPONSE>>>>", response);
                        responseComplete = new Intent(Constants.RESPONSE_SUCCESS);
                        responseComplete.putExtra("response", response);
                }, new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {
                responseComplete = new Intent(Constants.RESPONSE_ERROR);
  • \$\begingroup\$ Why are you trying to reinvent the wheel? Just use Timer and TimerTask (developer.android.com/reference/java/util/Timer.html). This way you won't need the service at all. Generally speaking I avoid spinning my own implementation if there is an existing API that helps me solve the issues I'm faced with. \$\endgroup\$ – Ivanhoe Sep 30 '16 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.