Android Lesson 11: work with AsyncTask


To keep things simple, we can understand AsyncTask like AJAX that we already know.

We create a simple example. In this example we are going to load a website in the background and display it in a WebView.

Step 1: create new android project.

Step 2: file AndroidManifest.xml, add

<uses-permission android:name="android.permission.INTERNET" />

to set permission to use Internet.

Step 3: Create the layout xml: res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.tutorialspots.asynctask.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TUTORIALSPOTS.COM"
        android:textColor="#FF0000"
        android:textSize="30dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load website"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_below="@+id/button"
          />
</RelativeLayout>

android asynctask example layout

Step 4: file MainActivity.java

Method 1:

package com.tutorialspots.asynctask;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button button;
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        webView = (WebView) findViewById(R.id.webView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DemoAsyncTask demoAsyncTask = new DemoAsyncTask();
                demoAsyncTask.execute(new String[]{"http://tutorialspots.com"});
            }
        });
    }

    public class DemoAsyncTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... strings) {
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(strings[0]);
            return null;
        }
    }
}

Method 2:

package com.tutorialspots.asynctask;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button button;
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        webView = (WebView) findViewById(R.id.webView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DemoAsyncTask demoAsyncTask = new DemoAsyncTask();
                demoAsyncTask.execute(new String[]{"http://tutorialspots.com"});
            }
        });
    }

    public class DemoAsyncTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... strings) {
            return strings[0];
        }

        @Override
        protected void onPostExecute(String s) {
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(s);
        }
    }
}

Step 5: run to test

android asynctask example

1 Comment

Leave a Reply