Learn about Android Content Providers and SQLite

Similar documents
Manifest.xml. Activity.java

Android Beginners Workshop

Android Workshop: Model View Controller ( MVC):

Database Development In Android Applications

Content Provider. Introduction 01/03/2016. Session objectives. Content providers. Android programming course. Introduction. Built-in Content Provider

8/30/15 MOBILE COMPUTING. CSE 40814/60814 Fall How many of you. have implemented a command-line user interface?

LifeStreet Media Android Publisher SDK Integration Guide

Simple Currency Converter

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

App Development for Android. Prabhaker Matet

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

<uses-permission android:name="android.permission.internet"/>

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

Android Services. Victor Matos Cleveland State University. Services

Notification mechanism

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Applications. Marco Ronchetti Università degli Studi di Trento

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

Android Application Development. By : Shibaji Debnath

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

MAD ASSIGNMENT NO 3. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Basic UI elements: Android Buttons (Basics) Marco Ronchetti Università degli Studi di Trento

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Computer Science E-76 Building Mobile Applications

Change in Orientation. Marco Ronchetti Università degli Studi di Trento

Created By: Keith Acosta Instructor: Wei Zhong Courses: Senior Seminar Cryptography

App Development for Smart Devices. Lec #7: Audio, Video & Telephony Try It Out

Agenda. The Android GUI Framework Introduction Anatomy A real word example Life cycle Findings

EMBEDDED SYSTEMS PROGRAMMING Android NDK

Solving an Android Threading Problem

IPN-ESCOM Application Development for Mobile Devices. Extraordinary. A Web service, invoking the SOAP protocol, in an Android application.

Getting started: Installing IDE and SDK. Marco Ronchetti Università degli Studi di Trento

Security model. Marco Ronchetti Università degli Studi di Trento

CS371m - Mobile Computing. Content Providers And Content Resolvers

CSE 660 Lab 7. Submitted by: Arumugam Thendramil Pavai. 1)Simple Remote Calculator. Server is created using ServerSocket class of java. Server.

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

Intents. Your first app assignment

A Crash Course to Android Mobile Platform

Android Overview. Most of the material in this section comes from

Software Practice 3 Before we start Today s lecture Today s Task Team organization

Create Parent Activity and pass its information to Child Activity using Intents.

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Android Development Tutorial. Yi Huang

1 카메라 1.1 제어절차 1.2 관련주요메서드 1.3 제작철차 서피스뷰를생성하고이를제어하는서피스홀더객체를참조해야함. 매니페스트에퍼미션을지정해야한다.

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Our First Android Application

Arrays of Buttons. Inside Android

ANDROID USER INTERFACE

App Development for Smart Devices. Lec #9: Advanced Topics

LECTURE NOTES OF APPLICATION ACTIVITIES

10.1 Introduction. Higher Level Processing. Word Recogniton Model. Text Output. Voice Signals. Spoken Words. Syntax, Semantics, Pragmatics

Android for Java Developers Dr. Markus Schmall, Jochen Hiller

else if(rb2.ischecked()) {

Basic GUI elements - exercises

App Development for Smart Devices. Lec #18: Advanced Topics

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

M.C.A. Semester V Subject: - Mobile Computing (650003) Week : 2

Vienos veiklos būsena. Theory

UNDERSTANDING ACTIVITIES

M.A.D ASSIGNMENT # 2 REHAN ASGHAR BSSE 15126

Tabel mysql. Kode di PHP. Config.php. Service.php

Android HelloWorld - Example. Tushar B. Kute,

Lecture 7: Data Persistence : shared preferences. Lecturer : Ali Kadhim Al-Bermani Mobile Fundamentals and Programming

COMP61242: Task /04/18

ITU- FAO- DOA- TRCSL. Training on. Innovation & Application Development for E- Agriculture. Shared Preferences

Android UI Development

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

Embedded Systems Programming - PA8001

Fragment Example Create the following files and test the application on emulator or device.

Android Application Model I

Mobile Programming Lecture 10. ContentProviders

Topics of Discussion

M O B I L E T R A I N I N G. Beginning Your Android Programming Journey

Intents, Intent Filters, and Invoking Activities: Part I: Using Class Name

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

Mobile Application Development Android

Real-Time Embedded Systems

Advanced Android Development

Tłumaczenie i adaptacja materiałów: dr Tomasz Xięski. Na podstawie prezentacji udostępnionych przez Victor Matos, Cleveland State University.

University of Stirling Computing Science Telecommunications Systems and Services CSCU9YH: Android Practical 1 Hello World

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department. Mobile Computing ECOM Eng. Wafaa Audah.

Services. Marco Ronchetti Università degli Studi di Trento

Tutorial: Setup for Android Development

Introduction To Android

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University. Overview (Review)

Android Media. Pro. Developing Graphics, Music, Video and Rich Media Apps for Smartphones and Tablets

Android & iphone. Amir Eibagi. Localization

android:orientation="horizontal" android:layout_margintop="30dp"> <Button android:text="button2"

Multiple Activities. Many apps have multiple activities

Gauthier Picard. MINES Saint-Étienne. October 10, 2017

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Designing Apps Using The WebView Control

Android Data Storage

Starting Another Activity Preferences

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University

Transcription:

Tampa Bay Android Developers Group Learn about Android Content Providers and SQLite Scott A. Thisse March 20, 2012

Learn about Android Content Providers and SQLite What are they? How are they defined? How are they called? What do they return? SQLite March 20, 2012 Tampa Bay Android Developers Group 2 of 32

What Are They? Mechanism used to encapsulate data into services Required when sharing data between apps Use REST-like URIs March 20, 2012 Tampa Bay Android Developers Group 3 of 32

How Are They Defined? Registered in AndroidManifest.xml Name is called an authority 3 rd Party ones have FQNs Example: <provider android:name="myprovider" android:authorities="org.tbadg.myprovider" /> March 20, 2012 Tampa Bay Android Developers Group 4 of 32

How Are They Called? URIs are used Authority is used as a base name similar to how a domain name is used content:// is used as the scheme URI can identify an individual item or an entire collection March 20, 2012 Tampa Bay Android Developers Group 5 of 32

How Are They Called? Input parameters are embedded in URI Each URI path segment must be predefined, documented, and interpreted by the provider Examples: content://org.tbadg.myprovider/ content://contacts/people/ content://contacts/people/13/ March 20, 2012 Tampa Bay Android Developers Group 6 of 32

What Do They Return? Data returned as an Android cursor Callers expected to know structure of the returned data Returned data structured with a predefined MIME type MIME type returned for a given URI if requested via gettype() March 20, 2012 Tampa Bay Android Developers Group 7 of 32

MIME Types Work similar as they do in HTTP MIME type returned as 2-part string in the form <type>/<subtype> Type part for indivual items is always vnd.android.cursor.item Type part for collections is always vnd.android.cursor.dir March 20, 2012 Tampa Bay Android Developers Group 8 of 32

MIME Types Subtypes are more flexible Prefix with vnd for nonstandard, vendor-specific types Examples: vnd.android.cursor.item/ vnd.tbadg.info vnd.android.cursor.dir/ vnd.tbadg.info March 20, 2012 Tampa Bay Android Developers Group 9 of 32

Accessing Data To reiterate, all access is thru URIs Base URIs (and columns) are usually defined with Java constants Easiest access is via managed query Built-in data requires a permissions entry in AndroidManifest.xml March 20, 2012 Tampa Bay Android Developers Group 10 of 32

Managed Query Takes 5 parameters: Base URI Optional Array of properties (columns) Optional Constraint clause (where) Optional replacement values Optional sort statement Returns a Cursor object March 20, 2012 Tampa Bay Android Developers Group 11 of 32

Managed Query Examples With URI based constraint: Cursor cur = managedquery( com.someprovider.app/entries/13, null, null, null, null); Without constraints or sort: Cursor cur = managedquery(android.provider.browser.bookmarks_uri, null, null, null, null); With a property (columns) array: new String[] {"title"}, null, null, null); March 20, 2012 Tampa Bay Android Developers Group 12 of 32

Managed Query Examples (cont) With constraints (where) clause: null, Browser.BookmarkColumns.BOOKMARK + " = 1", null, null); With replacement value: null, "bookmark =?", new String[] {"1"}, null); With sort statement: null, null, null, "bookmark ASC title DESC"); March 20, 2012 Tampa Bay Android Developers Group 13 of 32

Cursors Need to know names and type of columns Columns are identified by number in methods Random access collection of data rows Use movetofirst() first and then movetonext() and isafterlast() to navigate thru data March 20, 2012 Tampa Bay Android Developers Group 14 of 32

Inserts, Updates, Deletes (oh my) Handled via a ContentResolver and a ContentValues dictionary Populate ContentValues with row's new or changed values, then Call appropriate ContentResolver method Example: ContentValues cv = new ContentValues(); ContentResolver cr = getcontentresolver(); Uri uri = cr.insert(some_provider_uri, cv); March 20, 2012 Tampa Bay Android Developers Group 15 of 32

Extending/Building Content Providers Future presentation(s) will show extending and building custom content providers See references for assistance before then March 20, 2012 Tampa Bay Android Developers Group 16 of 32

Live Example AndroidManifest.xml main.xml strings.xml ContentProviders.java March 20, 2012 Tampa Bay Android Developers Group 17 of 32

AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.tbadg" android:versioncode="1" android:versionname="1.0"> <uses-sdk android:minsdkversion="7" /> <uses-permission android:name="com.android.browser.permission.read_history_bookmarks" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".contentproviders" android:label="@string/app_name"> <intent-filter> <category android:name="android.intent.category.launcher" /> <action android:name="android.intent.action.main" /> </intent-filter> </activity> </application> </manifest> March 20, 2012 Tampa Bay Android Developers Group 18 of 32

main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> continued... <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ToggleButton android:id="@+id/bookmarks" android:layout_width="0dp" android:layout_weight="33" android:layout_height="wrap_content" android:texton="@string/show_bookmarks_str" android:textoff="@string/noshow_bookmarks_str" android:onclick="onclick"> </ToggleButton> March 20, 2012 Tampa Bay Android Developers Group 19 of 32

main.xml (cont)...continued <ToggleButton android:id="@+id/history" android:layout_width="0dp" android:layout_weight="33" android:layout_height="wrap_content" android:texton="@string/show_history_str" android:textoff="@string/noshow_history_str" android:onclick="onclick"> </ToggleButton> <ToggleButton android:id="@+id/sort" android:layout_width="0dp" android:layout_weight="33" android:layout_height="wrap_content" android:texton="@string/sorted_str" android:textoff="@string/unsorted_str" android:onclick="onclick"> </ToggleButton> </LinearLayout> continued... March 20, 2012 Tampa Bay Android Developers Group 20 of 32

main.xml (cont)...continued <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/output_str" /> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/output" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout> March 20, 2012 Tampa Bay Android Developers Group 21 of 32

strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">contentproviders</string> <string name="show_bookmarks_str">bookmarks</string> <string name="noshow_bookmarks_str">no Bookmarks</string> <string name="show_history_str">history</string> <string name="noshow_history_str">no History</string> <string name="sorted_str">sorted</string> <string name="unsorted_str">unsorted</string> <string name="output_str"><b><u>output:</u></b></string> </resources> March 20, 2012 Tampa Bay Android Developers Group 22 of 32

package org.tbadg; ContentProviders.java import android.app.activity; import android.database.cursor; import android.os.bundle; import android.provider.browser; import android.view.view; import android.widget.textview; import android.widget.togglebutton; public class ContentProviders extends Activity { // Some handles to UI components: TextView _outputtvw = null; ToggleButton _bookmarksbtn = null; ToggleButton _historybtn = null; ToggleButton _sortbtn = null; @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } // Set up UI component handles: _outputtvw = (TextView)findViewById(R.id.output); _bookmarksbtn = (ToggleButton)findViewById(R.id.bookmarks); _historybtn = (ToggleButton)findViewById(R.id.history); _sortbtn = (ToggleButton)findViewById(R.id.sort); continued... March 20, 2012 Tampa Bay Android Developers Group 23 of 32

ContentProviders.java (cont)...continued continued... public void onclick(view v) { String where = null; String sort = null; // Reset the output: _outputtvw.settext(null); // Determine (a bit redundantly) the data wanted: if (_bookmarksbtn.ischecked()) if (_historybtn.ischecked()) // Both wanted where = null; else // Just bookmarks where = Browser.BookmarkColumns.BOOKMARK + " = 1"; else // Just history if (_historybtn.ischecked()) where = Browser.BookmarkColumns.BOOKMARK + " <> 1"; else // neither return; March 20, 2012 Tampa Bay Android Developers Group 24 of 32

ContentProviders.java (cont)...continued // Determine whether to sort the output (via the query): if (_sortbtn.ischecked()) sort = Browser.BookmarkColumns.TITLE + " ASC"; else sort = null; // Get the data from the browser content provider: Cursor cur = managedquery(android.provider.browser.bookmarks_uri, null, where, null, sort); int titleindx = cur.getcolumnindex(browser.bookmarkcolumns.title); } } // Walk thru the data and add it to the output: cur.movetofirst(); while (!cur.isafterlast()) { _outputtvw.append(cur.getstring(titleindx) + "\n"); cur.movetonext(); } March 20, 2012 Tampa Bay Android Developers Group 25 of 32

Screenshot March 20, 2012 Tampa Bay Android Developers Group 26 of 32

SQLite Most common CP data store Installed on all Android devices Open source, embedded database Databases implemented as flat-files Executable is named sqlite3 March 20, 2012 Tampa Bay Android Developers Group 27 of 32

Some Built-in Databases AlarmClock Browser CallLog Contacts MediaStore Settings March 20, 2012 Tampa Bay Android Developers Group 28 of 32

Some Useful SQLite Commands.mode column enable column output.headers on enable column headers.output <file> redirect output to file.output stdout redirect output to screen.show display current options.help display command help March 20, 2012 Tampa Bay Android Developers Group 29 of 32

Some Useful SQLite Commands (cont).schema display all database info.tables display table info.indexes display index info.dump export database objects.read <file> import dumped data.exit exit the interpreter March 20, 2012 Tampa Bay Android Developers Group 30 of 32

Q & A Questions? March 20, 2012 Tampa Bay Android Developers Group 31 of 32

References Allen, G. and Owens M. (2010). The Definitive Guide to SQLite, Second Edition. New York, New York: Apress Content Providers (n.d.). Retrieved March 8, 2012, from http://developer.android.com/guide/topics/providers/content-providers.html Komatineni, S. et al. (2011). Pro Android 3. New York, New York: Apress Murphy, M. (2012). The Busy Coder's Guide to Advanced Android Development. Retrieved March 8, 2012 from http://commonsware.com March 20, 2012 Tampa Bay Android Developers Group 32 of 32