CS185c
Chris Pollett
Oct 4, 2014
<html>
<head>
<title>Local Storage Test</title>
</head>
<body>
<script type="text/javascript">
if (localStorage) {
document.write("<h1>Local Storage is Working!</h1>");
testStorage();
} else {
document.write("<h1>Local Storage is Unavailable!</h1>");
}
function testStorage()
{
myvalue = localStorage.getItem("mykey1");
myvalue2 = localStorage["mykey2"];
if(myvalue) {
document.write("<p>Found stored value for mykey1 ="+
myvalue+"</p>");
localStorage.removeItem("mykey1");
//can use localStorage.clear(); to get rid of everything
} else {
localStorage.setItem("mykey1", "hello");
}
if(myvalue2) {
document.write("<p>Found stored value for mykey2 ="+
myvalue2+"</p>");
} else {
localStorage["mykey2"] = "yo";
}
document.write("<h1>List all that's stored after update</h1>");
for (i=0; i<=localStorage.length-1; i++)
{
key = localStorage.key(i);
value = localStorage.getItem(key);
document.write("<p>("+key+", "+value+")</p>");
}
}
</script>
</body>
</html>
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);
}
});
}
}
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
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
setTimeout(myCallback, repeatTimeInMilliSec); // myCallback is the Javascript function you would like called