Android Programming Tutorials

(Romina) #1
Sitting in the Background

Figure 18. The Run Long Task menu item

Step #4: Manage the Progress Bar.......................................................


Finally, we need to actually make use of the progress indicator. This


involves making it visible when we start our long-running task, updating it


as the task proceeds, and hiding it again when the task is complete.


First, make it visible by updating onOptionsItemSelected() to show it:


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.toast) {
String message="No restaurant selected";

if (current!=null) {
message=current.getNotes();
}

Toast.makeText(this, message, Toast.LENGTH_LONG).show();

return(true);
}
else if (item.getItemId()==R.id.run) {

73
Free download pdf