Local Storage, Tasks




CS185c

Chris Pollett

Mar 5, 2012

Outline

HTML 5 Local Storage

Timing Events

Java Code for Timing Events Demo.

package org.pollett.TimerTest;

import org.pollett.TimerTest.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Handler;



public class TimerTest extends Activity {
    Handler handler = new Handler(); //used to set up a delayed callback
    SeekBar seekBar; //object associated with SeekBar on our Layour
    TextView progressLabel; //object associated with our TextView label
    AlertDialog alertDialog; //object used to hold our Alert

    //Runnable object called after delay via Handler
    Runnable alertTask = new Runnable() {
        public void run() {
            alertDialog.show();
        }
    };


    
    /** 
        Called when the activity is first created. 
        Sets up all our the event listeners used in our example
    */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  
        //set up the seekBar events to modify the label value
        progressLabel  = (TextView)this.findViewById(R.id.TextView01);
        seekBar = (SeekBar)this.findViewById(R.id.SeekBar01);
        
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
            	progressLabel.setText(""+progress);
				
			}
        	public void onStartTrackingTouch(SeekBar seekBar) {	
        		//we're not doing anything here
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
        		//we're not doing anything here
			}


        });

        //set up our Alert Dialog but don't display it yet
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("You've waited long enough!").setCancelable(true);
        alertDialog = builder.create();
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                return;
        } });

        // set up button, when button is clicked, use handler to schedule a task
        Button waitButton = (Button)this.findViewById(R.id.Button01);	    
        waitButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                handler.removeCallbacks(alertTask);
                handler.postDelayed(alertTask, seekBar.getProgress()*1000);
	    }
        });        
    }    
}

Quiz

Which of the following is true?

  1. One parameter of the iOS's willAnimateRotationToInterfaceOrientation method controls whether or not the rotation is animated.
  2. The SQLiteOpenHelper class is a concrete class with many useful methods for executing SQL on Android.
  3. Android SharedPreferences get stored in the file system as xml files.

Timing on the iPhone

A view with a button on it

The Interface

@interface NSTimerDemoViewController : UIViewController {
	NSTimer *timer;
}
-(void) delayedResponse:(NSTimer*)theTimer;
-(IBAction) buttonPressed: (id) sender;
@end

New code in the Controller

-(IBAction) buttonPressed:(id) sender
{
	timer = [NSTimer scheduledTimerWithTimeInterval: 5.0 target:self 
		selector:@selector(delayedResponse:) userInfo:nil repeats:NO];


}

-(void) delayedResponse:(NSTimer*)theTimer {
	UIAlertView *alert = [[UIAlertView alloc]
            initWithTitle:@"Button Pressed -- Took A while" 
	    message:@"You pressed the button, but I was napping" 
	    delegate:nil 
	    cancelButtonTitle:@"Yep, I did" 
	    otherButtonTitles:nil];
	[alert show];
	[alert release];
}

After writing the above code, we connect the button up to buttonPressed in interface builder and the program should work.

Timing in Javascript

Multiview Applications

More on Multiview Applications

Creating Our View Controller and Nib Files

Modifying the App Delegate Header

Modifying the App Delegate Implementation

SwitchViewController.h