Local Storage, Tasks




CS185c

Chris Pollett

Oct 4, 2014

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.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 Layout
    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.activity_timer_test);
  
        //set up the seekBar events to modify the label value
        progressLabel  = (TextView)this.findViewById(R.id.textView1);
        seekBar = (SeekBar)this.findViewById(R.id.seekBar1);
        
        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(AlertDialog.BUTTON_POSITIVE, "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.button1);	    
        waitButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                handler.removeCallbacks(alertTask);
                handler.postDelayed(alertTask, seekBar.getProgress()*1000);
	    }
        });        
    }    
}

Timing on the iPhone

A view with a button on it

The Interface

Here is the code of our interface. Notice since timer is not a property but a field we put it in braces.

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
    NSTimer *timer;
}
-(void) delayedResponse:(NSTimer*)theTimer;
-(IBAction) buttonPressed: (id) sender;

@end

Implementation

Here is the new code in the Controller. To complete the project we need to control-drag to hook up button pressed with the button in the storyboard.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(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];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Timing in Javascript

Multiview Applications

More on Multiview Applications