Thursday, 31 October 2013

AsyncTask Example

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.


Example:




Code:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Start" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.asyntask;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView tv;
Button btn;
ProgressBar pb;
ProgressDialog dialog;
int i =0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView)findViewById(R.id.textView1);
btn = (Button)findViewById(R.id.button1);
pb = (ProgressBar)findViewById(R.id.progressBar1);
pb.setMax(100);

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new As().execute(i);
i++;
}
});

}

class As extends AsyncTask<Integer, Integer, Integer>
{
int i;
@Override
protected Integer doInBackground(Integer... params) {
// TODO Auto-generated method stub

while(i<100)
{
try
{
Thread.sleep(50);
}
catch(Exception e)
{
e.printStackTrace();
}
i++;
publishProgress(i);

Log.i("MO",String.valueOf(i)+": "+String.valueOf(params[0]));


}
return null;

}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
tv.setText(String.valueOf(values[0]));
pb.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("Download in progress");
dialog.setMessage("Please wait....");
dialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
dialog.dismiss();
super.onPostExecute(result);
}



}

}

Download full project from here 




Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

No comments:

Post a Comment