SlideShare a Scribd company logo
1 of 2
/*********************************************************************************************
*
* AsyncTaskSample.java
*
* Sample Java Code
*
* This file shows an implementation of an Asynchronous Task Object for use in an
* Android project. It does not perform any specific tasks.
*
* This sample code is Copyright by Matt Kutschera, 2017
*
* It may viewed by anyone, and may be distributed by LinkedIn only on my personal
* LinkedIn profile. Any other method of distribution is available only by approval on
* a case by case basis.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
**********************************************************************************************/
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.widget.TextView;
import java.util.LinkedList;
public class AsyncTaskSample extends AsyncTask<String, Void, Integer> {
protected LinkedList<String> strings;
private TextView outputTextView;
/**
* AsyncTaskSample()
*
* Initializes the strings LinkedList
*
* @param textView textView object to write results to
*/
public AsyncTaskSample(@NonNull TextView textView)
{
strings = new LinkedList<String>();
outputTextView = textView;
}
/**
* doInBackground()
*
* Performs some time-consuming task in the background. As this is simply a sample,
* it artificially takes up some time using the wait() function.
*
* @param params at least one string to write to the textView object
* @return number of strings to write to textView
*/
@Override
protected Integer doInBackground(String... params)
{
int i;
for (i = 0; i < params.length; i++)
{
strings.add(params[i]);
try
{
wait(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return params.length;
}
/**
*
* onPostExecute()
*
* Writes the result of the task to the TextView object.
*
* @param result number of strings to write to textView
*/
@Override
protected void onPostExecute(Integer result)
{
super.onPostExecute(result);
String header = result.toString() + " strings.n";
outputTextView.setText(header);
for (String string: strings)
{
outputTextView.append(string + "n");
}
}
}

More Related Content

Similar to androidSample

modernizr-1.5.js! Modernizr JavaScript library 1.5 .docx
modernizr-1.5.js!  Modernizr JavaScript library 1.5 .docxmodernizr-1.5.js!  Modernizr JavaScript library 1.5 .docx
modernizr-1.5.js! Modernizr JavaScript library 1.5 .docx
raju957290
 
jpf.jpgmodernizr-1.5.js! Modernizr JavaScript libra.docx
jpf.jpgmodernizr-1.5.js!  Modernizr JavaScript libra.docxjpf.jpgmodernizr-1.5.js!  Modernizr JavaScript libra.docx
jpf.jpgmodernizr-1.5.js! Modernizr JavaScript libra.docx
priestmanmable
 
Commenting in Agile Development
Commenting in Agile DevelopmentCommenting in Agile Development
Commenting in Agile Development
Jan Rybák Benetka
 
! Modernizr v2.0.6 httpwww.modernizr.com Copyri.docx
!  Modernizr v2.0.6  httpwww.modernizr.com   Copyri.docx!  Modernizr v2.0.6  httpwww.modernizr.com   Copyri.docx
! Modernizr v2.0.6 httpwww.modernizr.com Copyri.docx
MARRY7
 
case3h231diamond.gifcase3h231energy.jpgcase3h231moder.docx
case3h231diamond.gifcase3h231energy.jpgcase3h231moder.docxcase3h231diamond.gifcase3h231energy.jpgcase3h231moder.docx
case3h231diamond.gifcase3h231energy.jpgcase3h231moder.docx
tidwellveronique
 
Twanda.Malcolm12-6-16back.jpgTwanda.Malcolm12-6-16barchart.docx
Twanda.Malcolm12-6-16back.jpgTwanda.Malcolm12-6-16barchart.docxTwanda.Malcolm12-6-16back.jpgTwanda.Malcolm12-6-16barchart.docx
Twanda.Malcolm12-6-16back.jpgTwanda.Malcolm12-6-16barchart.docx
marilucorr
 

Similar to androidSample (20)

modernizr-1.5.js! Modernizr JavaScript library 1.5 .docx
modernizr-1.5.js!  Modernizr JavaScript library 1.5 .docxmodernizr-1.5.js!  Modernizr JavaScript library 1.5 .docx
modernizr-1.5.js! Modernizr JavaScript library 1.5 .docx
 
jpf.jpgmodernizr-1.5.js! Modernizr JavaScript libra.docx
jpf.jpgmodernizr-1.5.js!  Modernizr JavaScript libra.docxjpf.jpgmodernizr-1.5.js!  Modernizr JavaScript libra.docx
jpf.jpgmodernizr-1.5.js! Modernizr JavaScript libra.docx
 
Treatment, Architecture and Threads
Treatment, Architecture and ThreadsTreatment, Architecture and Threads
Treatment, Architecture and Threads
 
Android Meetup Slovenia #5 - Don't go crashing my heart by Zeljko Plesac, Inf...
Android Meetup Slovenia #5 - Don't go crashing my heart by Zeljko Plesac, Inf...Android Meetup Slovenia #5 - Don't go crashing my heart by Zeljko Plesac, Inf...
Android Meetup Slovenia #5 - Don't go crashing my heart by Zeljko Plesac, Inf...
 
Infinum Android Talks #14 - How (not) to get f***** by checkstyle, pdm, findb...
Infinum Android Talks #14 - How (not) to get f***** by checkstyle, pdm, findb...Infinum Android Talks #14 - How (not) to get f***** by checkstyle, pdm, findb...
Infinum Android Talks #14 - How (not) to get f***** by checkstyle, pdm, findb...
 
How do I Write Testable Javascript so I can Test my CF API on Server and Client
How do I Write Testable Javascript so I can Test my CF API on Server and ClientHow do I Write Testable Javascript so I can Test my CF API on Server and Client
How do I Write Testable Javascript so I can Test my CF API on Server and Client
 
Commenting in Agile Development
Commenting in Agile DevelopmentCommenting in Agile Development
Commenting in Agile Development
 
How do I write Testable Javascript so I can Test my CF API on Server and Client
How do I write Testable Javascript so I can Test my CF API on Server and ClientHow do I write Testable Javascript so I can Test my CF API on Server and Client
How do I write Testable Javascript so I can Test my CF API on Server and Client
 
Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2
 
Browser_Stack_Intro
Browser_Stack_IntroBrowser_Stack_Intro
Browser_Stack_Intro
 
Microsoft word java
Microsoft word   javaMicrosoft word   java
Microsoft word java
 
Basic overview of Angular
Basic overview of AngularBasic overview of Angular
Basic overview of Angular
 
! Modernizr v2.0.6 httpwww.modernizr.com Copyri.docx
!  Modernizr v2.0.6  httpwww.modernizr.com   Copyri.docx!  Modernizr v2.0.6  httpwww.modernizr.com   Copyri.docx
! Modernizr v2.0.6 httpwww.modernizr.com Copyri.docx
 
Unit Testing on Android - Droidcon Berlin 2015
Unit Testing on Android - Droidcon Berlin 2015Unit Testing on Android - Droidcon Berlin 2015
Unit Testing on Android - Droidcon Berlin 2015
 
case3h231diamond.gifcase3h231energy.jpgcase3h231moder.docx
case3h231diamond.gifcase3h231energy.jpgcase3h231moder.docxcase3h231diamond.gifcase3h231energy.jpgcase3h231moder.docx
case3h231diamond.gifcase3h231energy.jpgcase3h231moder.docx
 
NestJS
NestJSNestJS
NestJS
 
Hello worldswing.java
Hello worldswing.javaHello worldswing.java
Hello worldswing.java
 
If Hemingway Wrote JavaDocs
If Hemingway Wrote JavaDocsIf Hemingway Wrote JavaDocs
If Hemingway Wrote JavaDocs
 
Twanda.Malcolm12-6-16back.jpgTwanda.Malcolm12-6-16barchart.docx
Twanda.Malcolm12-6-16back.jpgTwanda.Malcolm12-6-16barchart.docxTwanda.Malcolm12-6-16back.jpgTwanda.Malcolm12-6-16barchart.docx
Twanda.Malcolm12-6-16back.jpgTwanda.Malcolm12-6-16barchart.docx
 
Readme
ReadmeReadme
Readme
 

androidSample

  • 1. /********************************************************************************************* * * AsyncTaskSample.java * * Sample Java Code * * This file shows an implementation of an Asynchronous Task Object for use in an * Android project. It does not perform any specific tasks. * * This sample code is Copyright by Matt Kutschera, 2017 * * It may viewed by anyone, and may be distributed by LinkedIn only on my personal * LinkedIn profile. Any other method of distribution is available only by approval on * a case by case basis. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * **********************************************************************************************/ import android.os.AsyncTask; import android.support.annotation.NonNull; import android.widget.TextView; import java.util.LinkedList; public class AsyncTaskSample extends AsyncTask<String, Void, Integer> { protected LinkedList<String> strings; private TextView outputTextView; /** * AsyncTaskSample() * * Initializes the strings LinkedList * * @param textView textView object to write results to */ public AsyncTaskSample(@NonNull TextView textView) { strings = new LinkedList<String>(); outputTextView = textView; } /** * doInBackground() * * Performs some time-consuming task in the background. As this is simply a sample, * it artificially takes up some time using the wait() function. * * @param params at least one string to write to the textView object * @return number of strings to write to textView */ @Override protected Integer doInBackground(String... params) { int i; for (i = 0; i < params.length; i++) { strings.add(params[i]); try { wait(100); } catch (InterruptedException e) {
  • 2. e.printStackTrace(); } } return params.length; } /** * * onPostExecute() * * Writes the result of the task to the TextView object. * * @param result number of strings to write to textView */ @Override protected void onPostExecute(Integer result) { super.onPostExecute(result); String header = result.toString() + " strings.n"; outputTextView.setText(header); for (String string: strings) { outputTextView.append(string + "n"); } } }