I am trying AsyncTask
. I cannot understand how to return the result.I am also confused on doInBackground()
has return
type Void
. Why does it need return null
; if I return null
how will I get value from this method.
package com.example.shikkok_services;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MyTask extends AsyncTask<Void, Integer, Void> {
//progressBar holo UI te kaj kore tai Context dorkar tai MyTask e construtor decler korlam.Inner class hole eita dorkar silo na
Context context;
Handler handler;
Dialog dialog;
TextView txtprogrss;
ProgressBar progress;
Button btnCancel;
MyTask(Context context, Handler handler){
this.context=context;
this.handler=handler;
}
MyTask(Context context){
this.context=context;
this.handler=handler;
}
//--------------------------------onPreExecute()............................................
@Override
protected void onPreExecute() {
super.onPreExecute();
// create dialog
dialog=new Dialog(context);
dialog.setCancelable(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.pogressdialog);
dialog.show();
txtprogrss=(TextView) dialog.findViewById(R.id.txtProgress);
progress=(ProgressBar)dialog.findViewById(R.id.progressBar2);
btnCancel=(Button)dialog.findViewById(R.id.btnProgress);
//progress=new ProgressBar(context);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyTask.this.cancel(true);
dialog.dismiss();
}
});
dialog.show();
}
//--------------------------------doInBackground()...........................
@Override
protected Void doInBackground(Void... arg0) {
//Kaj hobe backgournd e not UI e
// do tasks and update msg
for (int i = 0; i < 10; i++) {
if(isCancelled()){
break;
}else{
Log.e("In Background","current value;"+ i);
publishProgress(i);
//joto bar publishProgress() call hobe toto bar OnProgressUpdate hobe..
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
//------------------------------onProgressUpdate().....................................
@Override
protected void onProgressUpdate(Integer... values) {
//dowonload hole koto percent hosse seita dhore dhore UI te dekhalte parbo
super.onProgressUpdate(values);
// update dialog progress
progress.setProgress(values[0]);
txtprogrss.setText("progress update"+ values[0]+"%");
}
//-----------------------------OonPostExecute()...........................................
@Override
protected void onPostExecute(Void result) {
// jokhon kaj ses hoe jabe tokhon ei method e asben then UI te chole asben
super.onPostExecute(result);
// handler.sent message
dialog.dismiss();
Toast.makeText(context, "Finished", Toast.LENGTH_LONG).show();
}
}