Android: How to work with background thread? - android

I've developed an application that takes content from the internet and shows it accordingly on the device's screen . The program works just fine , a little bit slow . It takes about 3-4 seconds to load and display the content . I would like to put all the code that fetches the content and displays it in a background thread and while the program is doing those functions , I would like to display a progress dialog. Could you help me do this ? I would like especially to learn how to put the code in a background thread.
MY CODE
public class Activity1 extends Activity
{
private ProgressDialog progressDialog;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AsyncTask<Integer, Integer, Boolean>()
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute()
{
/*
* This is executed on UI thread before doInBackground(). It is
* the perfect place to show the progress dialog.
*/
progressDialog = ProgressDialog.show(Activity1.this, "",
"Loading...");
}
#Override
protected Boolean doInBackground(Integer... params)
{
if (params == null)
{
return false;
}
try
{
/*
* This is run on a background thread, so we can sleep here
* or do whatever we want without blocking UI thread. A more
* advanced use would download chunks of fixed size and call
* publishProgress();
*/
Thread.sleep(params[0]);
// HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
}
catch (Exception e)
{
Log.e("tag", e.getMessage());
/*
* The task failed
*/
return false;
}
/*
* The task succeeded
*/
return true;
}
#Override
protected void onPostExecute(Boolean result)
{
progressDialog.dismiss();
/*
* Update here your view objects with content from download. It
* is save to dismiss dialogs, update views, etc., since we are
* working on UI thread.
*/
AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this);
b.setTitle(android.R.string.dialog_alert_title);
if (result)
{
b.setMessage("Download succeeded");
}
else
{
b.setMessage("Download failed");
}
b.setPositiveButton(getString(android.R.string.ok),
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dlg, int arg1)
{
dlg.dismiss();
}
});
b.create().show();
}
}.execute(2000);
new Thread()
{
#Override
public void run()
{
// dismiss the progressdialog
progressDialog.dismiss();
}
}.start();
}
}

Check ASyncTask, its specifically created for such tasks.

public Runnable NameOfRunnable = new Runnable()
{
#Override
public void run()
{
while (true)
{
// TODO add code to refresh in background
try
{
Thread.sleep(1000);// sleeps 1 second
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
now start this with
Thread name = new Thread(NameOfRunnable);
name.start();

How to work with Background Thread.
Note: Do not work with UI with this background thread.
AsyncTask.execute(new Runnable() {
#Override
public void run() {
//TODO background code
}
});
Hope this would help you.

Related

Struggling with upating my UI from Thread

I have tried AsyncTask, Handler, and a simple Thread to achieve what I am trying to do but I just cannot get any of them to work, below is the logic that I need to use to update my UI...
public class GameProcessor extends Thread {
#Override
public void run() {
for (Integer integer : sequence) {
//set button state to pressed
Console.getBottomLeft().getButton().setBackgroundResource(R.drawable.button_focused);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//set button state to un-pressed
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Please do not respond with, you cannot update the UI from anywhere but the main thread, I already know this and need a solution on how to loop through some values from the backend while updating the UI at the same time. From what I can tell, AsyncTask and Handler will not help much.
Any help will be very appreciated!
If you know about the UI thread, why don't you:
runOnUiThread(new Runnable() {
public void run() {
//set button state to un-pressed or pressed.. or whatever you want..
}
});
I don't understand your problem
Create a member Handler object in your Activity (mHandler). Whenever you want to update your UI from the other thread, call
mHandler.post(new Runnable(){
public void run(){
//update the button state
}
});
The Handler will call this run() method in the UI thread for you.
This is simplified. You will probably want to create your Runnables as member variables so your aren't recreating the same Runnables over and over.
If you want to loop through some valuse, while updating the UI at the same time, then you may consider using AsyncTask and may use this feature:
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
And from :
protected Long doInBackground(URL... urls) {
calculate value
publishProgress(value);
return totalSize;
}
This will keep on updating UI thread with intermediate values you send.
In case you already know this and have tried and it does not solve your purpose, am sorry :)
or you can try this:
public void run() {
Console.post(new Runnable() {
public void run() {
Console.getBottomLeft().getButton().setBackgroundResource(R.drawable.button_focused);
}
});
}
In order to update your UI thread you can use the handler. Here's a simple example of using AsyncTask and Handler:
private static final String MESSAGE_KEY = "com.example.mypackage.MSGKEY";
private static final int MESSAGE_AUTHENTICATING = 0;
private static final int MESSAGE_AUTHENTICATED = 1;
/**
* This handler will update UI
*
*/
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.getData().getInt(MESSAGE_KEY)) {
case MESSAGE_AUTHENTICATING:
hashstream_stream.setVisibility(View.GONE);
hashstream_progress.setVisibility(View.VISIBLE);
break;
case MESSAGE_AUTHENTICATED:
hashstream_stream.setVisibility(View.VISIBLE);
hashstream_progress.setVisibility(View.GONE);
break;
default:
break;
}
}
};
/**
* This method should be used to update UI thread.
*
* #param value
*/
private void postMessage(int value) {
Message msgObj = handler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putInt(MESSAGE_KEY, value);
msgObj.setData(bundle);
handler.sendMessage(msgObj);
}
/**
* AsyncTask Helper class as network op
*
*
*/
private class StreamHashTagTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
//Do actual operation in here
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
postMessage(MESSAGE_AUTHENTICATED);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
postMessage(MESSAGE_AUTHENTICATING);
}
/**
* If you need to update progress override onProgressUpdate() method.
* Since I am indeterminate using progress bar as authentication time
* cannot be calculated , I don't need update here
*/
}
Did you try?
instanceOfActivity.runOnUiThread(new Runnable(){
#Override
public void run() {
Console.getBottomLeft().getButton().setBackgroundResource(R.drawable.button_focused);
}
});
But in this case, I don't suggest you use an activity in another object.
Please using interface like above:
public interface INotifyChange {
void notify(object value); // Example: void notify(int progress);
}
In your activity where invoke GameProcessor
INotifychange mNotifier;
mNotifier = new INotifyChange() {
#Override
public void notify(object value) {
runOnUiThread(new Runnable(){
#Override
public void run() {
//You can update your UI here.
}
});
}
};
// And your GameProcessor
private INotifyChange mNotifier;
public GameProcessor(INotifyChange aNotifier) {
mNotifier = aNotifier;
}
// Where you want to update UI, please invoke
mNotifier.notify(value);

How to update view after done with the progress bar?

CASE: I have a button and list-view in the activity. On click of the button I have added a click listener, which starts a new thread in which I update the progress bar. After the job is done i.e. progress bar is done 100%, I want to update the list-view.
final OnClickListener mStartScan = new OnClickListener() {
#Override
public void onClick(View v) {
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(false);
progressBar.setMessage(getString(R.string.text_scanning_inbox));
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(totalSms);
progressBar.show();
progressBarStatus = 0;
Thread progressThread = new Thread(new Runnable() {
public void run() {
while (progressBarStatus < totalSms) {
// process some tasks
progressBarStatus = someStuff();
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= done) {
// sleep 1 seconds, so that you can see the 100%
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
// this method updates the list
populateList();
}
}
});
progressThread.start();
// try {
// progressThread.join();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } finally {
// populateList();
// }
}
};
PROBLEM: When I update the listview after completion of the task and dismissing progress bar, I get an exception which says that the view can be updated only from the thread in which it is created.
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
WHAT ELSE I TRIED: I tried waiting for the thread which is running progress bar to complete and then update listview from the main thread.
try {
progressThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
populateList();
}
However, this does not work. It does not show the progress bar at all.
I used this:
private ProgressDialog progressBar;
class MyTask extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
progressBar = new ProgressDialog(getApplicationContext());
progressBar.setMessage("please, waiting ...");
progressBar.setCancelable(false);
progressBar.show();
}
#Override
protected String doInBackground(String... params) {
try {
// get info and set them in my model ...
} catch (Exception e) {
e.printStackTrace();
}
return params[0];
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (null != progressBar && progressBar.isShowing()) {
progressBar.dismiss();
}
// do work in UI and set info adapter and refresh list ...
populateList();
myListView.invalidateViews();
}
}
for more information:
http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged%28%29
Only the original thread that created a view hierarchy can touch its views.
Here original thread refers to the ui thread. You are attempting to update ui inside a thread which is not possible.
You cannot update ui from the back ground thread. You can use runOnUiThread .
runOnUiThread(new Runnable() //run on ui threa
{
public void run()
{
}
});
I would suggest you to use asynctask
You use use asynctask for this purpose. The onPreExecute(), onPostExecute() are invoked on the ui thread and you can use the same to update ui. You can do your background computation in doInbackground()
http://developer.android.com/reference/android/os/AsyncTask.html
Check the topic under heading The 4 steps.
class TheTask extends AsyncTask<Void,Void,Void>
{
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// background computation and publish progress
return null;
}
#Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
// update progress bar
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// cancel the progress bar
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
//display progress bar
}
}
Load asynctask on the ui thread
new TheTask().execute().
The error you are receiving is because of the fact that you are trying to change the UI components on a secondary thread. You should read this for more information on how to use threads on Android.
You can use the Activity method runOnUIThread() to call the populateList() method or whatever makes the updates on the UI(main) thread. If you read the doc mentioned above, you will find out more about this.

progress Dialog simpel

i want to add a progress Dialog button when i click on this button before the new activity apperar, i think i don't need a thread, i did search but i find only that i need to do a thread and many other think it s not clear
i just want when i clik on a progress Dialog say to the user to wait so a few sec the other activity will appear that's all:
btn_newsfeed.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Launching News Feed Screen
Intent i = new Intent(getApplicationContext(), CustomizedListView.class);
startActivity(i);
}
});
There are three different different ways in which you can use a ProgressDailog -using threads, handlers and async tasks.
here a example of async task for using a progress Dialog
private class Operation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params)
{
// code to be executed in background thread
for(int i=0;i<5;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
// runs on UI thread and updated UI after executing doInBackground
progressDialog.dismiss();
}
#Override
protected void onPreExecute() {
ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Title ", "Loading...");
progressDialog.show();
}
#Override
protected void onProgressUpdate(Void... values) {
// runs on UI thread and starts first
}
}

Android progress dialog

My application fetches some html code from the internet and when done , displays it on the devices screen. Since it takes about 3-4 seconds to do that , in this time the screen stays black , I'd like to use a progress dialog. This is my code :
package com.nextlogic.golfnews;
// ALL THE IMPORTS ....
public class Activity1 extends Activity {
private ProgressDialog progressDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
progressDialog = ProgressDialog.show(Activity1.this, "", "Loading...");
new Thread()
{
public void run()
{
try
{
sleep(2000);
// HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
}
catch (Exception e)
{
Log.e("tag",e.getMessage());
}
// dismiss the progressdialog
progressDialog.dismiss();
}
}.start();
The program works but it doesn't display anything anymore. I have one error in logcat :
Only the original thread that created a view hierarchy can touch its views.
Could you please help me ? Thanks in advance.
The error is explicative enough. To update one visual object you must run the changes inside main thread. A quick and dirty fix could be calling the update code inside runOnUiThread().
However in your case I would use an AsyncTask to download and update the progress of the progress bar. The task has the property to run on UI thread when it ends (so you can update the views there, such as dismissing the progress dialog)
Here is an example how to use an AsyncTask to display a download progress dialog.
Update
Stackoverflow already has the answers to all your question. Here is an example of an AsyncTask to download some content and display the download progress. Just what you want.
Update 2
Ok here is your code using an AsyncTask:
public class Activity1 extends Activity
{
private ProgressDialog progressDialog;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AsyncTask<Integer, Integer, Boolean>()
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute()
{
/*
* This is executed on UI thread before doInBackground(). It is
* the perfect place to show the progress dialog.
*/
progressDialog = ProgressDialog.show(Activity1.this, "",
"Loading...");
}
#Override
protected Boolean doInBackground(Integer... params)
{
if (params == null)
{
return false;
}
try
{
/*
* This is run on a background thread, so we can sleep here
* or do whatever we want without blocking UI thread. A more
* advanced use would download chunks of fixed size and call
* publishProgress();
*/
Thread.sleep(params[0]);
// HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
}
catch (Exception e)
{
Log.e("tag", e.getMessage());
/*
* The task failed
*/
return false;
}
/*
* The task succeeded
*/
return true;
}
#Override
protected void onPostExecute(Boolean result)
{
progressDialog.dismiss();
/*
* Update here your view objects with content from download. It
* is save to dismiss dialogs, update views, etc., since we are
* working on UI thread.
*/
AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this);
b.setTitle(android.R.string.dialog_alert_title);
if (result)
{
b.setMessage("Download succeeded");
}
else
{
b.setMessage("Download failed");
}
b.setPositiveButton(getString(android.R.string.ok),
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dlg, int arg1)
{
dlg.dismiss();
}
});
b.create().show();
}
}.execute(2000);
new Thread()
{
#Override
public void run()
{
// dismiss the progressdialog
progressDialog.dismiss();
}
}.start();
}
}
You need to do this way
runOnUiThread(new Runnable() {
public void run() {
// Do Your Stuff
}});
Dismiss your dialog like this:
Handler handler = new Handler();
handler.post(new Runnable(){
public void run(){
progressDialog.dismiss();
}
});
Create a UI thread after completing network operation
runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
}
});
The top answer works great, so here is an example to implement an AsyncTask in MonoDroid (thanks to Greg Shackels): http://mono-for-android.1047100.n5.nabble.com/AsyncTask-td4346647.html

Show Dialog After Thread is Finish

I want show dialog after finish Thread.
In thread I am changing TextView's Value like as 0 to 100...
When TextView Value is reach 100 then i want to show dialog..
What i do for it.
Thanks in advance...
Code Snippet:
final Thread thread = new Thread(new Runnable()
{
#Override
public void run()
{
synchronized (this)
{
try
{
for(int i=0 ; i<speed; i++)
{
final int value=i+1;
wait(3000/speed);
Test.this.runOnUiThread(new Runnable() {#Override public void run()
{
accText.setText(String.valueOf(value));
}});
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread.start();
this is asyntask code snippet...
class setTextBackgroundTask extends AsyncTask<String , Integer, Void>
{
#Override
protected void onPreExecute()
{
}
#Override
protected Void doInBackground(String... params)
{
Thread th = new Thread();
int value;
for(int i=0 ; i<speed; i++)
{
value=i+1;
publishProgress(value);
try {
th.sleep(3000/speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values)
{
accText.setText(String.valueOf(values[0]));
System.out.println("Value=="+values[0]);
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(Void result)
{
showShareDialog();
}
}
This document explains how you can create dialogs.
Please note that you will have to do any dialog creation code on the UI thread.
You already have code that runs something on the UI thread, just do that outside the loop,but with the dialog creation code inside.
You should use AsyncTask: subclass AsyncTask, override doInBackground() to execute your time consuming action on another thread, and then override onPostExecute() to show your dialog.
Note that you cannot change UI elements from a non-UI (background) thread. AsyncTask takes care of that for you: it calls doInBackground() on a new thread and then calls onPostExecute() on the UI thread as soon as the background task is complete.

Categories

Resources