THGTechGuy
THGTechGuy

Reputation: 59

Run New Thread on Timer Android

I've been working on an android app which regularly checks a mysql database using JSON and everything works fine with my code.

Im having trouble running this as a timer as it only runs once and then stops. The only code i managed to get working runs the http request on the UI thread which freezes up. Any help would be most appreciated. Thank in advance,

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    checkUpdate.start();
    ...
}

private Thread checkUpdate = new Thread() {
    public void run() {
        try {
            // my code here to get web request to return json string
        } 

        String response = httpclient.execute(httppost, responseHandler);
                    mHandler.post(showUpdate);
    }
    ...
}


private Runnable showUpdate = new Runnable(){
    public void run(){
        try{
            // my code here handles json string as i need it
            Toast.makeText(MainActivity.this,"New Job Received...", Toast.LENGTH_LONG).show();
            showja();
        }
    }
}


private void showja(){
    Intent i = new Intent(this, JobAward.class);  
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish();   
}

Upvotes: 1

Views: 316

Answers (1)

Nate
Nate

Reputation: 31045

As @Raghunandan suggested, the standard way to perform work in the background on Android, and then modify the UI when that work is done, is using AsyncTask.

First define a new subclass of AsyncTask:

private class JsonRequestTask extends AsyncTask<HttpUriRequest, Void, String> {
     protected String doInBackground(HttpUriRequest... requests) {
         // this code assumes you only make one request at a time, but
         //   you can easily extend the code to make multiple requests per
         //   doInBackground() invocation:
         HttpUriRequest request = requests[0];

         // my code here to get web request to return json string

         String response = httpclient.execute(request, responseHandler);
         return response;
     }

     protected void onPostExecute(String jsonResponse) {
        // my code here handles json string as i need it
        Toast.makeText(MainActivity.this, "New Job Received...", Toast.LENGTH_LONG).show();
        showja();  
     }
 }

and then you would use the task like this, instead of your Thread:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    JsonRequestTask task = new JsonRequestTask();
    task.execute(httppost);
    ...
}

You may run the task again by simply creating a new JsonRequestTask() and calling its execute() method.

A common practice for a simple async task like this is to make it a private inner class within the Activity class that uses it (if only one Activity needs it). You may need to change the scope of some of your activity's variables so that the inner class may use them (e.g. move local variables to member variables).

Upvotes: 1

Related Questions