More Persistence, Android Timing and Widgets




CS185c

Chris Pollett

Sep. 29, 2010

Outline

Introduction

More Intro

Android FileSystem Test (Read Part)

Android FileSystem Test (Write Part)

FileOutputStream fos = null;
try
{
   fos = this.openFileOutput("filename.txt", Context.MODE_PRIVATE);
   fos.write("hi there\n".getBytes());
   Log.i("AndroidPersistence", "I just wrote stuff\n");
}
catch (IOException ie)
{
   Log.e("AndroidPersistence", "File Write Error");
}
finally
{
   if(fos !=null) 
   {
      try {fos.close();} catch(Exception e) {}
   }
}

SQLite

Database Commands

SQLite on the iPhone

Executing Commands on an SQLite Database on the iPhone

Android Database Persistence

On Testing Code

Subclassing SQLiteOpenHelper

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);
	    }
        });        
    }    
}