I'd like to brand the AsyncTask class for doing something in background updating the caller thread with the progress but with the caller thread != of UI thread. I've tried that code but the line publishProgress(i) seems have not effect. Can someone tell me how can I fix it (or what other class can I use else). Thanks in advance =)
public class MainUI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Thread t=new Thread(){
boolean exit=false;
public void run(){
Looper.prepare();
new DownloadFilesTask().execute();
while (!exit){
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d("","Exit thread");
Looper.loop();
}
public void exit(){
exit=true;
}
class DownloadFilesTask extends AsyncTask<Void, Long, Long> {
protected Long doInBackground(Void... urls) {
long i=0;
for (i=0;i<20;i++){
Log.d("",i+" ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(i);
}
return i;
}
protected void onProgressUpdate(Long... progress) {
Log.d("Test",progress[0]+"");
}
protected void onPostExecute(Long result) {
exit();
}
}
};
t.start();
}
});
}
}