1

I develop android app use GCM, i want to send heartbeat for every 5 minutes at once, i create it when service is run,these are my codes,

public class sendHeartbeat extends Service{
    private Handler customHandler = new Handler();
    private static final String TAG = "MyService";

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

    @Override
    public void onCreate() {

        Log.d(TAG, "onCreate");
    }

    @Override
    public void onStart(Intent intent, int startId) {

        Log.d(TAG, "onStart");  
        customHandler.postDelayed(updateTimerThread, 0);
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
    }

    private Runnable updateTimerThread = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            new syncGetHeartbeat().execute();
            Log.d("send hearbeat","send hearrrrrrrbeat");

            customHandler.postDelayed(this, 260000);
        }
    };


    class syncGetHeartbeat extends AsyncTask<Void, Void, String> { 
       @Override
        protected String doInBackground(Void... unsued) {
            try {                       
                String varId="naengoland";
                String url = "http://mydomain.com/gcmserver/getHeartbeat.php?id="+varId; 
                getRequestJSON(url);

                return null;
            } catch (Exception e) {
                Log.e(e.getClass().getName(), "service get hertbeat "+ e.toString());
                return null;
            }

        }

        @Override
        protected void onProgressUpdate(Void... unsued) {

        }

        @Override
        protected void onPostExecute(String sResponse) {

        }
    }

    public String getRequestJSON(String Url){
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(Url);
        try{
          client.execute(request);
        }catch(Exception ex){
        }
        return null;
    }
}

I successfully send the heartbeat for every 5 minutes, but the problem is my app running slowly and consume battery a lot, is there any way to send heartbeat beside doing my way? i mean,the better method than my method.

Please help.

Coderji
  • 7,655
  • 5
  • 37
  • 51
ltvie
  • 931
  • 4
  • 19
  • 48

1 Answers1

0

Using TimerTask provides better performance than using Thread (ref).. However TimerTask isn't good in handling multiple threads at the same time which isn't a concern in your case because you are doing one task repeatedly.

here is the code for Timer:

first initialize a Timer:

private Timer timer;

in onCreate:

timer = new Timer();

then within onStart:

timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            new syncGetHeartbeat().execute();
            Log.d("send hearbeat","send hearrrrrrrbeat");

        }
    }, 0, 300000); //means start at 0 second and each 5 mins (300000 ms) run the task

I'm not sure if there is another solution that has better performance however this is absolutely better than running thread in a fixed rate.

Community
  • 1
  • 1
Coderji
  • 7,655
  • 5
  • 37
  • 51