2
\$\begingroup\$

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;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

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

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

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

@Override
public void onDestroy() {
    super.onDestroy();
    if (service.isRunning) {
        service.interrupt();
        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() {
        @Override
        public void run() {
            isRunning = true;
            while (isRunning) {
                try {
                    Log.d("HANDLER_RUNNABLE>>>>", "HANDLER");
                    sendRequest();
                    Thread.sleep(milliSecs);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    isRunning = false;
                }
            }
        }
    };

    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public void run() {
        super.run();
        runTask.run();
    }

    private void sendRequest() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.URL_HOME,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("RESPONSE>>>>", response);
                        responseComplete = new Intent(Constants.RESPONSE_SUCCESS);
                        responseComplete.putExtra("response", response);
                        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(responseComplete);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                responseComplete = new Intent(Constants.RESPONSE_ERROR);
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(responseComplete);
            }
        });
        VolleyController.getInstance().addToRequestQueue(stringRequest);
    }
}}
\$\endgroup\$
  • \$\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.