Demo:
1)Activity:
package com.venky.asynctask;
3)Download this Project Click Here
1)Activity:
package com.venky.asynctask;
import
android.app.Activity;
import
android.os.AsyncTask;
import
android.os.Bundle;
import
android.os.SystemClock;
import
android.widget.ProgressBar;
import
android.widget.TextView;
import
android.widget.Toast;
public class
AsyncTask2Activity extends Activity {
/** Called when the activity is first
created. */
ProgressBar progressBar;
TextView tv;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar = (ProgressBar)
findViewById(R.id.progressBar2);
tv = (TextView) findViewById(R.id.textView1);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
class MyAsyncTask extends
AsyncTask<Void, Integer, Void> {
int progress_status;
@Override
protected void onPreExecute()
{
// TODO
Auto-generated method stub
progress_status = 0;
tv.setText("0
%");
super.onPreExecute();
}
@Override
protected Void
doInBackground(Void... params) {
// TODO
Auto-generated method stub
while (progress_status < 100) {
progress_status += 2;
publishProgress(progress_status);
SystemClock.sleep(300);
}
return null;
}
@Override
protected void
onProgressUpdate(Integer... values) {
// TODO
Auto-generated method stub
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
tv.setText("Download
" + values[0] + " %" + " complete");
}
@Override
protected void
onPostExecute(Void result) {
// TODO
Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Download
Complete",
Toast.LENGTH_LONG).show();
tv.setText("Download
Complete");
super.onPostExecute(result);
}
}
}
2)main.xml:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#5F04B4"
android:orientation="vertical"
>
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginTop="70dp"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Medium
Text"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
3)Download this Project Click Here
No comments:
Post a Comment