Listing 125 Field Servicejava implements Field Service Activity

package com.msi.manning.UnlockingAndroid;

// multiple imports trimmed for brevity, see full source code public class FieldService extends Activity { final int ACT IVI TY_RE FRESH JOBS = 1 ;

final int act IVI t y_l I s t job s = 2; Useful constants final int ACT IVIT Y_S ETTINGS = 3 ;

Prefs myprefs = null; <—O Prefs instance

@Override public void onCreate(Bundle icicle) { Set up UI

super.onCreate(icicle);

setContentView(R.layout.fieldservice); <-

© Instantiate Prefs instance

Initiate UI field contents myprefs = new Prefs (this .getApplicationContext () ) ; <1-Ref reshUserInf o () ; <1~

final Button refreshjobsbutton = (Button) findViewByld(R.id.getjobs); refreshj obsbutton.setOnClickListener(new Button.OnClickListener() { public void onclick (View v) { Connect try { button to UI

startActivityForResult(new

Intent(v.getContext(),RefreshJobs.class),ACTIVITY_REFRESHJOBS);

E onActivityResult processing

// see full source comments

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) {

case ACTIVITY_REFRESHJOBS:

break; case ACTIVITY_LISTJOBS:

break; case ACTIVITY_SETTINGS:

RefreshUserInfo(); break;

private void Ref reshUserInf o () { <1—F RefreshUserInfo try {

final TextView emaillabel = (TextView) findViewById(R.id.emailaddresslabel);

emaillabel.setText("User: " + myprefs.getEmail() + "\nServer: " + myprefs.getServer() + "\n"); } catch (Exception e) {

This code implements a simple UI that displays three distinct buttons. As each is selected, a particular Activity is started in a synchronous, call/return fashion. The Activity is started with a call to startActivityForResult ©. When the called Activity is complete, the results are returned to the FieldService Activity via the onActivityResult method Q. An instance of the Prefs class O, © is used to obtain values for displaying in the UI. Updating the UI is accomplished in the method RefreshUserInfo ©.

Because the settings are so important to this application, the next section covers the management of the user and server values.

0 0

Post a comment