Discuss the role of AsyncTasks and provide an explanation of how you addressed the issue in the AsyncTask lab.
Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive.
Android application runs on a single thread when launched. Due to this single thread model tasks that take longer time to fetch the response can make the application non-responsive. To avoid this we use android AsyncTask to perform the heavy tasks in background on a dedicated thread and passing the results back to the UI thread. Hence use of AsyncTask in android application keeps the UI thread responsive at all times.
The basic methods used in an android AsyncTask class are defined below :
The three generic types used in an android AsyncTask class are given below :
Android AsyncTask Example
To start an AsyncTask the following snippet must be present in the MainActivity class :
MyTask myTask = new MyTask(); myTask.execute();
In the above snippet we’ve used a sample classname that extends AsyncTask and execute method is used to start the background thread.
Note:
In this tutorial we’ll implement an AsyncTask that makes a process to go to sleep for a given period of time as set by the user.
Android Async Task Project Structure

Android AsyncTask Example Code
The xml layout is defined in the activity_main.xml and its given below:
activity_main.xml
<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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10pt"
android:textColor="#444444"
android:layout_alignParentLeft="true"
android:layout_marginRight="9dip"
android:layout_marginTop="20dip"
android:layout_marginLeft="10dip"
android:text="Sleep time in Seconds:"/>
<EditText
android:id="@+id/in_time"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_toRightOf="@id/tv_time"
android:layout_alignTop="@id/tv_time"
android:inputType="number"
/>
<Button
android:id="@+id/btn_run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Run Async task"
android:layout_below="@+id/in_time"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="7pt"
android:layout_below="@+id/btn_run"
android:layout_centerHorizontal="true" />
</RelativeLayout>
In the above layout we’ve used a predefined drawable as the border of the EditText.
The MainActivity.java is defined below:
package com.journaldev.asynctask;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText time;
private TextView finalResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (EditText) findViewById(R.id.in_time);
button = (Button) findViewById(R.id.btn_run);
finalResult = (TextView) findViewById(R.id.tv_result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTaskRunner runner = new AsyncTaskRunner();
String sleepTime = time.getText().toString();
runner.execute(sleepTime);
}
});
}
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
publishProgress("Sleeping..."); // Calls onProgressUpdate()
try {
int time = Integer.parseInt(params[0])*1000;
Thread.sleep(time);
resp = "Slept for " + params[0] + " seconds";
} catch (InterruptedException e) {
e.printStackTrace();
resp = e.getMessage();
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
progressDialog.dismiss();
finalResult.setText(result);
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(MainActivity.this,
"ProgressDialog",
"Wait for "+time.getText().toString()+ " seconds");
}
@Override
protected void onProgressUpdate(String... text) {
finalResult.setText(text[0]);
}
}
}
In the above code we’ve used AsyncTaskRunner class to perform the AsyncTask operations. The time in seconds is passed as a parameter to the class and a ProgressDialog is displayed for the given amount of time.
Discuss the role of AsyncTasks and provide an explanation of how you addressed the issue in...
Is voter turnout in Texas a problem? If so how can this issue be addressed to increase voter participation? What institutions, organizations or groups help in increasing voter turnout and by what means? If you do not think this is a problem, what is your reasoning?
Why is disparity in healthcare an issue in the U.S.? How is it being addressed by the federal government?
Discuss the role of a Christian worldview in IT project management and provide an example of how it can be applied to contribute to the success of a project.
Discuss what issues you see as needing to be addressed in an organization in order to make organizational leadership changes more likely to be successful. As an industrial engineer, how can you influence the impact some of these changes might have on an organization’s overall quality management strategy?
discuss the nurse executives role in all hazards preparedness along with issue pertainin to level of alert benchmarks for preparedness role of long term care facilities in disaster plans disaster preparedness alliances and educational content
Identify the role of a health care Provide an explanation that illustrates manager in the function stated. Function the role of a health care manager as it applies to the function in the health care Industry Organizing Planning Controlling Leading
This discussion question should be addressed in these three steps: 1. Discuss how a manager can control employees to make necessary changes in an HCO. 2. How do you feel about using intuition rather than rational thinking to make decisions? 3. Compare and contrast radical change and incremental change. 4. Discuss how nonverbal communication is important for managers
discuss a typical budgeting process and how behavioural issues as well as uncertainties would be addressed.
Question 2: Discuss the role of technology in enabling virtualization in an organization. Provide specific instances of how technology and software contribute to virtualization.
Discuss what you see as the role of ethics as it pertains to management and managers. Does management, in your view, help shape the values and ethics of an organization? Is employee behavior, ethical or not, a by-product of the organization's ethical climate? What ideally is the manager's role in helping to create and maintain organizational integrity? Using your professional experience as a backdrop, provide examples of managers who have demonstrated ethical behavior (or not) and discuss how this affected...