Skip to main content

AsyncTask in Android.

    
       AsyncTask  is one Class which is used to perform background processes without having threads. For an AsyncTask is having 4 steps. onPreExecute, doInBackground, onProgressUpdate, onPostExecute

onPreExecute method is invoked first while calling the AsyncTask. Here comes the fuctions which are to be performed at the starting. The functions such as  showing progress dialog etc..

doInBackground method will execute after onPreExecute method. Here comes the functions which are to be done in background. The functions such as downloading files uploading files etc...

onProgressUpdate method is invoked while the progress is changed. Here we will write the code to perform the action while changing the progress.

onPostExecute method is invoked after the doInBackground method is completed. This method will execute at last. Here we will write code to perform the actions at last. The functions such as progress dialog dismiss, parse the result, redirection to another page etc..

Here is One Example:

 private class LoginProcessing extends AsyncTask<Object, Void, Void> {
         private LoginCredentials myLoginCredentials;
         private LoginToken loginToken;
         private ProgressDialog progressDialog;
      
         public LoginProcessing(LoginCredentials Credentials) {
                super();
                myLoginCredentials=Credentials;
         }

         protected void onPreExecute (){
             progressDialog = ProgressDialog.show(context, "", "Logging you in Reward World...",true);
         }
        
        @Override
        protected Void doInBackground(Object... arg0) {
            // TODO Auto-generated method stub
             try {
                   loginToken = PersistenceService.getService().login(myLoginCredentials);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            return null;      
        }
        protected void onPostExecute(Void result){
            progressDialog.dismiss();
            if(loginToken!=null){
                if(loginToken.isIsValidLogin()){
                    SaveToken(loginToken.getToken());
                    Intent intent = new Intent(context, HomeScreen.class);
                    startActivity(intent);
                    finish();
                }
                else{

                     ShowLoginFailedDialog(loginToken.getErrorOnFailure().toString());                   
                }
            }else{
                Message("Service Error");
            }
              
        }
        
     }
    

In this LoginProcessing LoginCredentials is the parameters passing to the login function.The LoginProcessing AsyncTask can be called by,

            new LoginProcessing(loginCredentials).execute();


Popular posts from this blog

Importing Image From Camera

In this tutorial I want to explain how to take a picture in camera ans import that picture to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will open Camera. After that the user can take photo After taking photo it will ask for save image. If the user save the image then it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_con…

How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.

In this blog I want to explain How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.


Intent
                Intent is an object that is used to launch an activity or to return information  from an activity.  To start a new activity using startActivity(intent).  To get information from another activity we start activity using startActivityForResult(intent, requestCode).


               Here in  FromPage.java we are adding  Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List values to an intent. In ToPage.java We extract added values will retrieve from intent. 


FromPage.java

ArrayList<Integer>  IntArrayList = new  ArrayList<Integer>(Arrays.asList(100,13,18,22,44,13));
ArrayList<String>  StringArrayList = new  ArrayList<String>(Arrays.asList("String1", "String2", "String3", "String4"));

        int[] intList = {4, 6, 3, …

Importing Image From Gallery

In this tutorial I want to explain how to import images from Gallery to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will redirect to Gallery to Browse image. While we select an image it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert Image" />

    <ImageView
        …