240 likes | 343 Vues
Embedded database engine, WebViews. Content taken from book: “Hello, Android” by Ed Burnette Third Edition. SQLite1. SQLite1 is a tiny yet powerful database engine created by Dr. Richard Hipp in 2000. The most widely deployed SQL database engine in the world.
E N D
Embedded database engine, WebViews Content taken from book: “Hello, Android” by Ed Burnette Third Edition
SQLite1 • SQLite1 is a tiny yet powerful database engine created by Dr. Richard Hipp in 2000. • The most widely deployed SQL database engine in the world. • Besides Android, SQLite can be found in the Apple iPhone, Symbian phones, Mozilla Firefox, Skype, PHP, Adobe AIR, Mac OS X, Solaris, and many other places.
SQLite Popularity • It’s free. • The authors have placed it in the public domain and don’t charge for its use. • It’s small. • The current version is about 150KB, well within the memory budget of an Android phone. • It requires no setup or administration. • There is no server, no config file, and no need for a database administrator.
SQLite database • It is just a file. • You can take that file, move it around, and even copy it to another system (for example, from your phone to your workstation), and it will work fine. • Android stores the file in the /data/data/packagename/databases directory
SQL • Instead of calling Java I/O routines to access this file from your program, you run Structured Query Language (SQL) statements. • To use a SQL database, you submit SQL statements and get back results. • There are three main types of SQL statements: DDL, Modification, and Query.
DDL Statements • A database file can have any number of tables. • A table consists of rows, and each row has a certain number of columns. • Each column of the table has a name and a data type (text string, number, and so forth). • You define these tables and column names by first running Data Definition Language (DDL) statements.
Create table create table mytable ( _id integer primary key autoincrement, name text, phone text ); • The _id column isn’t strictly required for SQLite, but later when we want to use an Android ContentProvider, we’ll need it.
Modification Statements • SQL provides a number of statements that let you insert, delete, and update records in the database. For example, to add a few phone nunmbers: insert into mytable values(null, 'Steven King' , '555-1212' ); insert into mytable values(null, 'John Smith' , '555-2345' ); insert into mytable values(null, 'Fred Smitheizen' , '555-4321' ); • The values are specified in the same order you used in the CREATE TABLE statement. • We specify NULL for _id because SQLite will figure that value out for us.
Query Statements • SELECT statement. • For example, if you wanted to get the third entry, you could do this: • select * from mytable where(_id=3); • Find all the records containing “Smith” in the name: • select name, phone from mytable where(name like "%smith%" );
Hello, Database Project name: Events Build Target: Android 2.2 Application name: Events Package name: org.example.events Create Activity: Events Min SDK Version: 8 Constants.Java It holds a few constants describing the database
Constants • Each event will be stored as a row in the events table. • Each row will have an _id, time, and title column. _id is the primary key, declared in • the BaseColumns interface that we extend. • time and title will be used for a time stamp and event title, respectively.
EventsData.java • A helper class called EventsData to represent the database itself. This class extends the Android SQLiteOpenHelper class, which manages database creation and versions. All you need to do is provide a constructor and override two methods.
EventsData.java The constructor starts on line 16. DATABASE_NAME is the actual filename of the database we’ll be using (events.db), and DATABASE_VERSION is just a number we make up.
Main.xml • This declares the TextView with an imaginative ID of text (R.id.text in code) and wraps it with a ScrollView <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </ScrollView>
Events activity • On line 20 of onCreate( ), we set the layout for this view. Then we create an instance of the EventsData class on line 21 and start a try block.
Running a Query • The getEvents( ) method does the database query to get a list of events: • Although we don’t use them in this example, the query( ) method has parameters to specify a WHERE clause, a GROUP BY clause, and a HAVING clause.
WebView • Android provides a wrapper around the WebKit browser engine called WebView that you can use to get the real power of a browser • WebView works pretty much like any other Android view except that it has a few extra methods specific to the browser
WebView Class • A View that displays web pages. • This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. • It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.
Methods • addJavascriptInterface( ): Allows a Java object to be accessed from JavaScript • createSnapshot( ): Creates a screenshot of the current page • getSettings( ): Returns a WebSettings object used to control the settings • loadData( ): Loads the given string data into the browser • loadDataWithBaseURL( ): Loads the given data using a base URL • loadUrl( ): Loads a web page from the given URL
Methods • setDownloadListener( ): Registers callbacks for download events, such as when the user downloads a .zip or .apk file • setWebChromeClient( ): Registers callbacks for events that need to be done outside the WebView rectangle, such as updating the title or progress bar or opening a JavaScript dialog box • setWebViewClient( ): Lets the application set hooks in the browser to intercept events such as resource loads, key presses, and authorization requests • stopLoading( ): Stops the current page from loading