Listing 1213 Refresh Jobsjava

package com.msi.manning.UnlockingAndroid; / / multiple imports omitted for brevity, see full source public class RefreshJobs extends Activity {

Figure 12.8 The ProgressDialog in use during RefreshJobs

Prefs myprefs = null; Boolean bCancel = false; JobList mList = null;

ProgressDialog progress; Progress

Handler progresshandler; y indicator @Override public void onCreate(Bundle icicle) {

setContentView (R. layout. refreshj obs) ; ProgressDialog myprefs = new Prefs(this.getApplicationContext); myprogress = ProgressDialog.show(this, "Refreshing Job List", ^ "Please Wait",true,false); O

progresshandler = new Handler () { <1—© Define Handler @Override public void handleMessage(Message msg) { ^^ y date UI with switch (msg_what) { E textualmessajie case 0 : <-1

myprogress.setMessage("" + (String) msg.obj);

break;

myprogress.cancel();

finish () ; © Handle cancel break; I and cancel with case 2 : // error occurred <|-' error myprogress.cancel();

super.handleMessage(msg); }

© Use openFilelnput _I for stream

Thread workthread = new Thread(new DoReadJobs ()); workthread.start();

Initiate DoReadJobs class instance

Looping construct

Prepare status message class DoReadJobs implements Runnable { public void run() {

InputSource is = null; © Create Message

Message msg = new Message () ; <-' object msg. what = 0 ; try {

msg.obj = (Object) ("Connecting ..."); progresshandler.sendMessage(msg) ; URL url = new URL (myprefs . getServer () +

^ "getjoblist.php?identifier=" + myprefs.getEmail()) ;

SAXParserFactory factory = SAXParserFactory.newlnstance(); SAXParser parser = factory.newSAXParser();

XMLReader xmlreader = parser. getXMLReader () ; Prepare to

JobListHandler jlHandler = parse data new JobListHandler (progresshandler) ; <1-1

xmlreader. setContentHandler (j lHandler) ; Instantiate msg = new Message () ; JobListHandler msg. what = 0 ;

progresshandler.sendMessage(msg); xmlreader.parse(is); msg = new Message () ; msg. what = 0 ;

msg.obj = (Object)("Parsing Complete"); progresshandler.sendMessage(msg); msg = new Message () ; msg. what = 0 ;

msg.obj = (Object)("Saving Job List"); progresshandler.sendMessage(msg); jlHandler.getList().persist() ; <— Persist data msg = new Message () ; msg. what = 0 ;

msg.obj = (Object)("Job List Saved."); progresshandler.sendMessage(msg);

msg = new Message () ; 1$ Set status flag msg. what = 1; <1-'

progresshandler.sendMessage(msg); } catch (Exception e) { Log.d("CH12","Exception: " + e.getMessage()); msg = new Message () ; msg.what = 2; // error occurred msg.obj = (Object)("Caught an error retrieving ^ Job data: " + e.getMessage()); progresshandler.sendMessage(msg);

for completion

Set status flag for error

A ProgressDialog O is used to display progress information to the user. There are a number of ways to display progress in Android. This is perhaps the most straightforward approach. A Handler is employed to process Message instances. While the Handler itself is defined as an anonymous class, the code requires a reference to it for passing to the JobListHandler when parsing, which is shown in [email protected] When instantiating the ProgressDialog Q, the arguments include:

0 0

Post a comment