Android – CoNTENT PRoViders

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

ListView Examples. Basic Steps for Creating a Listview 1.Create a layout (e.g., a LinearLayout), with elements for –the ListView (
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
ContentProviders.  Databases for reading & writing data  Support typical database operations  e.g., query, insert, update & delete.
Who Am I And Why Am I Here I’m professor Stephen Fickas in CIS – you can call me Steve. I have a research group that works with mobile devices (since 1995!)
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Cosc 5/4730 Android Content Providers and Intents.
컨텐트 프로바이더 박승제. Content Provider The only way to share data across applications Using content provider Use existing content providers supplied by android.
CONTENT PROVIDER. Content Provider  A content provider makes a specific set of the application's data available to other applications => Share data to.
Android Application Development with Java UPenn CS4HS 2011 Chris Murphy
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
Presenting Lists of Data. Lists of Data Issues involved – unknown number of elements – allowing the user to scroll Data sources – most common ArrayList.
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Data Storage: Part 1 (Preferences)
SQLite Database. SQLite Public domain database – Advantages Small (about 150 KB) – Used on devices with limited resources Each database contained within.
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
Data Storage: Part 3 (SQLite)
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College.
ANDROID – INTERFACE AND LAYOUT L. Grewe. Interfaces: Two Alternatives Code or XML  You have two ways you can create the interface(s) of your Application.
Content providers Accessing shared data in a uniform way 1Content providers.
Cosc 5/4730 Android Content Providers and Intents.
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
CS378 - Mobile Computing Content Providers And Content Resolvers.
Package org.androidtown.database.query; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;
Mobile Programming Lecture 6
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Mobile Computing Lecture#11 Adapters and Dialogs.
Address Book App 1. Define styles   Specify a background for a TextView – res/drawable/textview_border.xml.
Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
Announcements Homework #2 will be posted after class due Thursday Feb 7, 1:30pm you may work with one other person No office hours tonight (sorry!) I will.
Android App Basics Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5.
Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
Android - SQLite Database 12/10/2015. Introduction SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with.
Android and s Ken Nguyen Clayton state University 2012.
Address Book App 1 Fall 2014 CS7020: Game Design and Development.
David Sutton USING CONTENT PROVIDERS. TOPICS COVERED THIS WEEK  Persistence  Introduction to databases  Content providers  Cursors  Cursor adapters.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
Http :// developer. android. com / guide / topics / fundamentals. html.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
Content Providers.
Making content providers
Content provider.
Android Content Providers & SQLite
Data Storage: Part 4 (Content Providers)
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
Android Introduction Hello World
Content Providers And Content Resolvers
Android Application Development 1 6 May 2018
GUI Programming Fundamentals
ListView: Part 2.
Concurrency in Android
Android Introduction Hello World.
CS499 – Mobile Application Development
Content Providers.
ITEC535 – Mobile Programming
תכנות ב android אליהו חלסצ'י.
Android Programming Lecture 6
CIS 470 Mobile App Development
CIS 470 Mobile App Development
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android Sensor Programming
Presentation transcript:

Android – CoNTENT PRoViders L. Grewe

Content Provider A content provider makes a specific set of the application's data available to other applications. If you don't need to share data amongst multiple applications you can use a database directly via SQLiteDatabase.

Built-in Content Providers Contacts MediaStore.Audio MediaStore.Images MediaStore.Video Browser CallLog Settings

Basic Idea – URI format Data mapped to a URI (CONTENT_URI) URI is used to access data by client <standard_prefix>://<authority>/<data_path>/<id > examples: content://media/internal/images = list of all internal images on device content://media/external/images = list of all external images on device content://call_log/calls = list of all calls in Call Log content://browser/bookmarks = list of bookmarks

Basic Idea – example for Contacts Content Provider Data mapped to a URI (CONTENT_URI) URI is used to access data by client content://contacts/people/ All contact names content://contacts/people/23 Contact with _ID = 23

CONTENT_URI Content URI can be complex Rather than hardcoding exact URI everywhere Content Providers expose the base URI as static field NameProvider.CONTENT_URI public static final String AUTHORITY = “packagename.NameProvider"; public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/names");

Option 1: Accessing data with a Content Provider using CursorLoader CursorLoader cursor Loader = new CursorLoader(Context context, Uri uri, String[ ] projection,String selection, String[ ] selectionArgs, String sortOrder) context = associated context uri = Content Provider URI projection =which columns to return selection = SQL Where clause with "WHERE" selectionArgs =Arguments for selection sortOrder = SQL ORDER BY clause OPTION 1: This is for Honeycomb Or Later version of Android

Option1: Example Accessing Content Provider data with CursorLoader This example: we ask for all contacts Loads in background –does NOT block application UI import android.content.CursorLoader; // INSIDE Activity Class ******** @Override public void onCreate(Bundle savedInstances) { //*** other code***** Uri allContacts = Uri.parse(“content://contacts/people”); CursorLoader cursorLoader = new CursorLoader( this, allContacts, //URI of content provider null, //means return all columns null, // WHERE clause-- won't specify. null, // no where clause, so no arguments null); //no order by specified //get data from Content Provider, populates Cursor c with result set Cursor c = cursorLoader.loadInBackground(); //LOADS in background, no blocking

Option1: Example Accessing Content Provider data with CursorLoader This example: display results using a SimpleCursorAdapter import android.widget.SimpleCursorAdapter; import android.database.Cursor; import android.widget.CursorAdapter; import android.provider.ContactsContract; //built in contacts content provider info // INSIDE Activity Class ******** @Override public void onCreate(Bundle savedInstances) { //*** other code SEE PREVIOUS SLIDE***** results in c (Cursor instance) //info will display from ContentProvider results in Cursor c String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts._ID}; int[] views = new int[] {R.id.contactName, R.id.contactID}; adapter = new SimpleCursorAdapter(this, R.layout.main, c, columns, views CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER Means this adapter registered to be informed when change in content provider

Option1: Example ---- what is SimpleCursorAdapter An Adapter used to represent Cursor (data result set) Used to populate a related View  example …. android.app.ListActivity as one possibility public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to, int flags) context = context where the ListView associated with this layout = resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to" c = database cursor. from =list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet. to = views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. flags = Flags used to determine the behavior of the adapter, as per CursorAdapter(Context, Cursor, int). ListActivityInstance.setListAdapter(SimpleCursorAdapter_Instance)

Example Main.xml ---interface for app’s main Activity (a ListActivity) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/android:list" android:layout_height="wrap_content" android:layout_weight="1" android:stackFromBottom="false" android:transcriptMode="normal" /> <TextView android:id="@+id/contactName" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" /> android:id="@+id/contactID" android:layout_width="fill_parent" </LinearLayout>

Example --- main Activity Class public class ProviderActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Uri allContacts = ContactsContract.Contacts.CONTENT_URI; String[] projection = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER}; Cursor c; CursorLoader cursorLoader = new CursorLoader( this, allContacts, projection, ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?“, new String[] {"%Lee"}, ContactsContract.Contacts.DISPLAY_NAME + " ASC"); c = cursorLoader.loadInBackground(); String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts._ID}; int[] views = new int[] {R.id.contactName, R.id.contactID}; SimpleCursorAdapter adapter; adapter = new SimpleCursorAdapter( this, R.layout.main, c, columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); this.setListAdapter(adapter); PrintContacts(c); }

Example --- main Activity Class continued private void PrintContacts(Cursor c) { if (c.moveToFirst()) { do{ String contactID = c.getString(c.getColumnIndex( ContactsContract.Contacts._ID)); String contactDisplayName = c.getString(c.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); Log.v("Content Providers", contactID + ", " + contactDisplayName); } while (c.moveToNext()); } <<< UTILITY Method to print out the result set returned in the Cursor instance c that contains all the Contacts.

Example ---some explanation Predefined Query String Constants Uri allContacts = ContactsContract.Contacts.CONTENT_URI SAME AS Uri allContacts = Uri.parse(“content://contacts/people”);

Example ---some explanation Following is like saying give me all the contacts with the columns ID, DISPLAY_NAME and HAS_PHONE_NUMBER where the DISPLAY_NAME is Like Lee and orderby DISPLAY_NAME in Ascending order (ASC) String[] projection = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER}; Cursor c; CursorLoader cursorLoader = new CursorLoader( this, allContacts, projection, ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?“, new String[] {"%Lee"}, ContactsContract.Contacts.DISPLAY_NAME + " ASC");

Predefined Query Strings Predefined Query String Constants Uri allContacts = ContactsContract.Contacts.CONTENT_URI SAME AS Uri allContacts = Uri.parse(“content://contacts/people”); Other examples Browser.BOOKMARKS_URI Browser.SEARCHES_URI CallLog.CONTENT_URI MediaStore.Images.Media.INTERNAL_CONTENT_URI MediaStore.Images.Media.EXPERNAL_CONTENT_URI Settings.CONTENT_URI

Another option to query Content Provider Older method

Option 2: Example with managedQuery(*) Android.net.Uri allContacts; allContacts = Uri.parse(“content://contacts/people”); Cursor c = managedQuery(allContacts, null,null,null,null); //gets all contacts OPTION 2: ONLY FOR HISTORIC NEEDS!!!!! OPTION 2: This is for prior to Honeycomb Version of Android

Option 2: Accessing data with a Content Provider using manaedQuery(*) public final Cursor managedQuery(Uri uri, String[] projection,String selection, String[] selectionArgs, String sortOrder) uri = Content Provider URI projection =which columns to return selection = SQL Where clause with "WHERE" selectionArgs =Arguments for selection sortOrder = SQL ORDER BY clause OPTION 2: This is for prior to Honeycomb Version of Android