Saturday 26 January 2013

AsyncTask in android Example simple Program


AsyncTask Example Program :

AsyncTaskActivity.java

package com.example.asynctaskexample;

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

public class AsyncTaskActivity extends Activity {

    Button btn_Start ;
    ProgressBar progressbar ;
    TextView text_percentage ;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async_task);
       
      btn_Start = (Button) findViewById(R.id.btn_start) ;
      progressbar = (ProgressBar) findViewById(R.id.progress) ;
      text_percentage = (TextView) findViewById(R.id.txt_percentage) ;

     
      btn_Start.setOnClickListener(new OnClickListener() {
       
        @Override
        public void onClick(View v) {
           
            btn_Start.setEnabled(false) ;
            new ShowDialogAsyncTask().execute() ;
           
        }
    });
     
    }

   private class ShowDialogAsyncTask extends AsyncTask< Void , Integer , Void > {

       int progress_status ;
      
       protected void onPreExecute() {
           Log.v("hari","onPreExecute-------------") ;
           // update the UI immediately after the task is executed
           super.onPreExecute();
           
            Toast.makeText(AsyncTaskActivity.this,"Invoke onPreExecute()", Toast.LENGTH_SHORT).show();
       
            progress_status = 0;
            text_percentage.setText("downloading 0%");
           
          }
    @Override
    protected Void doInBackground(Void... params) {
       
        Log.v("hari","doInBackground-----------:params:"+params) ;
       
        while( progress_status < 100 ) {Log.v("hari","doInBackground-------while") ;
           
            progress_status += 2 ;
            publishProgress(progress_status);
            SystemClock.sleep(300);
        }
        return null;
    }
   
    protected void onProgressUpdate(Integer... values) {
       
           super.onProgressUpdate(values);
           Log.v("hari","onProgressUpdate-----------values:-"+values) ;
           progressbar.setProgress(values[0]);
           text_percentage.setText("downloading " +values[0]+"%");
           
          }
   
    protected void onPostExecute(Void result) {
           super.onPostExecute(result);
           Log.v("hari","onPostExecute------------") ;
            Toast.makeText(AsyncTaskActivity.this,"Invoke onPostExecute()", Toast.LENGTH_SHORT).show();
            
            text_percentage.setText("download complete");
            btn_Start.setEnabled(true);
          }
   }
   
}

UI :
activity_async_task.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
       
        android:text="Android Async Task"
         />

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="34dp" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progress"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:minWidth="120dp"
        android:text="start_btn" />

    <TextView
        android:id="@+id/txt_percentage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/progress"
        android:text="downloading  0%"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks for sharing the good information and post more information. I need some help for my website, Visit once
    software training company in Hyderabad

    ReplyDelete
  3. Android Automation UI BDD Project with Espresso Framework and Cucumber Tools with Gherkin Language https://www.youtube.com/watch?v=vZV6EiSJAfc&t=27s

    ReplyDelete
  4. https://www.youtube.com/watch?v=vZV6EiSJAfc&t=27s

    ReplyDelete
  5. Implementing Section Recycler view with Search Functionality in Android Studio Programmatically
    https://www.youtube.com/watch?v=xq1Ufpo9f1o

    ReplyDelete

Complete Details about eLitmus pH Test at Rs:699/- Share your java material and fresher interview Information for us to Help Others... mail to : vhkrishnan.v@gmail.com