Hello friends
Today i cover how to use AsynTask class.
actvity_main.xml
ActvityMain.java
Today i cover how to use AsynTask class.
actvity_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.samset.asyntaskexample.MainActivity">
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="Sleep time in milliseconds:"
android:textColor="#444444"
android:textSize="15dp" />
<EditText
android:id="@+id/et_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_time"
android:layout_marginTop="5dp"
android:inputType="text">
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/et_time"
android:text="Run Async task" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_task"
android:layout_marginLeft="9dip"
android:layout_marginTop="15dip"
android:text=""
android:textColor="#AA0000"
android:textSize="15dp" />
</RelativeLayout>
<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.samset.asyntaskexample.MainActivity">
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="Sleep time in milliseconds:"
android:textColor="#444444"
android:textSize="15dp" />
<EditText
android:id="@+id/et_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_time"
android:layout_marginTop="5dp"
android:inputType="text">
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/et_time"
android:text="Run Async task" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_task"
android:layout_marginLeft="9dip"
android:layout_marginTop="15dip"
android:text=""
android:textColor="#AA0000"
android:textSize="15dp" />
</RelativeLayout>
ActvityMain.java
package com.samset.asyntaskexample;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button btnStart;
private EditText time;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (EditText) findViewById(R.id.et_time);
btnStart = (Button) findViewById(R.id.btn_task);
result = (TextView) findViewById(R.id.tv_result);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// created instance of MyAsynTask class which extends
// AsyncTask
MyAsynTask runner = new MyAsynTask();
String sleepTime = time.getText().toString();
runner.execute(sleepTime); // time from editText
}
});
}
private class MyAsynTask extends AsyncTask<String, String, String> {
private String resp;
// this function run on seperate thread to do background work
// do your time taking work here
@Override
protected String doInBackground(String... params) {
publishProgress("Initilize...");
// onProgressUpdate function
try {
int time = Integer.parseInt(params[0]);
//Sleeping for given time period for 10 times
for (int i = 0; i < 10; i++) {
Thread.sleep(time);
publishProgress("Downloading..." + (i + 1));
}
resp = "Completed " + time;
} catch (InterruptedException e) {
e.printStackTrace();
resp = e.getMessage();
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
// return result to onPostExecute function
return resp;
}
@Override
protected void onPostExecute(String result) {
// result published on completion of doInBackground function
MainActivity.this.result.setText(result);
}
@Override
protected void onPreExecute() {
// Things to be done before execution of long running operation. For
}
@Override
protected void onProgressUpdate(String... text) {
result.setText(text[0]);
// Things to be done while execution of long running operation is in
// progress. For example updating ProgessDialog
}
}
}
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button btnStart;
private EditText time;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (EditText) findViewById(R.id.et_time);
btnStart = (Button) findViewById(R.id.btn_task);
result = (TextView) findViewById(R.id.tv_result);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// created instance of MyAsynTask class which extends
// AsyncTask
MyAsynTask runner = new MyAsynTask();
String sleepTime = time.getText().toString();
runner.execute(sleepTime); // time from editText
}
});
}
private class MyAsynTask extends AsyncTask<String, String, String> {
private String resp;
// this function run on seperate thread to do background work
// do your time taking work here
@Override
protected String doInBackground(String... params) {
publishProgress("Initilize...");
// onProgressUpdate function
try {
int time = Integer.parseInt(params[0]);
//Sleeping for given time period for 10 times
for (int i = 0; i < 10; i++) {
Thread.sleep(time);
publishProgress("Downloading..." + (i + 1));
}
resp = "Completed " + time;
} catch (InterruptedException e) {
e.printStackTrace();
resp = e.getMessage();
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
// return result to onPostExecute function
return resp;
}
@Override
protected void onPostExecute(String result) {
// result published on completion of doInBackground function
MainActivity.this.result.setText(result);
}
@Override
protected void onPreExecute() {
// Things to be done before execution of long running operation. For
}
@Override
protected void onProgressUpdate(String... text) {
result.setText(text[0]);
// Things to be done while execution of long running operation is in
// progress. For example updating ProgessDialog
}
}
}
Thank you
No comments:
Post a Comment