Android Asynchronous Task



Android Asynchronous Task


Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.


Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >

<Button
 android:id="@+id/startprogress"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Start"
 />
<ProgressBar
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="8dp"
 style="?android:attr/progressBarStyleHorizontal"
 android:id="@+id/progressbar_Horizontal"
 android:max="100"
 />
</LinearLayout>

Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;


    public class MainActivity extends Activity {
   
     ProgressBar progressBar;
   
     public class BackgroundAsyncTask extends
        AsyncTask<Void, Integer, Void> {
     
      int myProgress;

      @Override
      protected void onPostExecute(Void result) {
      
       Toast.makeText(MainActivity.this,
             "onPostExecute", Toast.LENGTH_LONG).show();
      
      }

      @Override
      protected void onPreExecute() {
      
       Toast.makeText(MainActivity.this,
             "onPreExecute", Toast.LENGTH_LONG).show();
       myProgress = 0;
      }

      @Override
      protected Void doInBackground(Void... params) {
      
       while(myProgress<100){
        myProgress++;
        publishProgress(myProgress);
           SystemClock.sleep(100);
       }
       return null;
      }

      @Override
      protected void onProgressUpdate(Integer... values) {
      
       progressBar.setProgress(values[0]);
      }

     }
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar)findViewById(R.id.progressbar_Horizontal);
        progressBar.setProgress(0);
    
       ((Button)findViewById(R.id.startprogress)).setOnClickListener(new View.OnClickListener(){
       
           @Override
              public void onClick(View v) {
       
               new BackgroundAsyncTask().execute();
       
              }
       });
     
    }
    }


Step: Open AndroidManifest.xml and add following code :


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
         />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5 : Our output will be like this :