Back to project page 2014-Droid-code.
The source code is released under:
GNU General Public License
If you think the Android project 2014-Droid-code listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.androidbook.simpleasync; /*/*from w w w. j a v a 2 s. c o m*/ * This class is the example from the book chapter you have on moodle * From Android Wireless Application Development Volume II Chapter 1 * * Uses Java Threads * Meant to contrast with AsyncTask do not implement in your code. * Must be managed by programmer. */ import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.widget.TextView; public class SimpleThreadActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.count); final TextView tv = (TextView) findViewById(R.id.counter); // instantiate the Thread with a Runnable() new Thread(new Runnable() { // start the thread public void run() { int i = 0; while (i < 100) { SystemClock.sleep(500); i++; final int curCount = i; if (curCount % 5 == 0) { /* * View.post(Runnable) causes * the Runnable to be added to the message queue. * The runnable will be run on the user interface thread */ tv.post(new Runnable() { public void run() { // update UI with progress every 5% tv.setText(curCount + "% Complete!"); } }); } } tv.post(new Runnable() { public void run() { tv.setText("Count Complete!"); } }); } }).start(); } }