AsyncTask Example Android with ProgressBar

MainActivity.java

package com.asynctaskexample;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
 
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
 
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
 
public class MainActivity extends Activity {
	private TextView textView;
	private ProgressBar progressBar;
	private DownloadWebPageTask mTask = null;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView = (TextView) findViewById(R.id.TextView01);
		downloadPage();
	}
 
	// AsyncTask <TypeOfVarArgParams , ProgressValue , ResultValue> .
	private class DownloadWebPageTask extends AsyncTask<String, Integer, String> {
 
		@Override
		protected void onPreExecute() {
			//textView.setText("Hello !!!");
			progressBar = (ProgressBar) findViewById(R.id.progressBar1);
			progressBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}
 
		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
 
		}
 
		@Override
		protected String doInBackground(String... urls) {
			String response = "";
			for (String url : urls) {
				DefaultHttpClient client = new DefaultHttpClient();
				HttpGet httpGet = new HttpGet(url);
				try {
					HttpResponse execute = client.execute(httpGet);
					InputStream content = execute.getEntity().getContent();
 
					BufferedReader buffer = new BufferedReader(new InputStreamReader(
							content));
					String s = "";
					while ((s = buffer.readLine()) != null) {
						response += s;
					}
 
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return response;
		}
 
		@Override
		protected void onPostExecute(String result) {
			progressBar.setVisibility(View.INVISIBLE);
			textView.setText(result);
		}
	}
 
	private void downloadPage() {
		if (mTask != null
				&& mTask.getStatus() != DownloadWebPageTask.Status.FINISHED) {
			mTask.cancel(true);
		}
               // execute(String[]) you can put array of links to web pages, or array of Integer[] 
               // if first param is Integer[] etc.
		mTask = (DownloadWebPageTask) new DownloadWebPageTask()
				.execute(new String[] { "http://android.okhelp.cz/android-market.html",
						"http://android.okhelp.cz/android-market.html" });
	}
 
	@Override
	protected void onDestroy() {
		super.onDestroy();
 
		if (mTask != null
				&& mTask.getStatus() != DownloadWebPageTask.Status.FINISHED) {
			mTask.cancel(true);
			mTask = null;
		}
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
 
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
 
</LinearLayout>

AndroidManifest.xml do not forget INTERNET uses-permission !!!!!!!

<uses-permission android:name="android.permission.INTERNET" />
This entry was posted in Android Examples Code and tagged . Bookmark the permalink.


Comments are closed.

30,798 views