SlidePub
Home
Categories
Login
Register
Home
Business
Content providers in Android
Content providers in Android
AlexeyUstenko
4,709 views
51 slides
Nov 10, 2012
Slide
1
of 51
Previous
Next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
About This Presentation
No description available for this slideshow.
Size:
280.48 KB
Language:
en
Added:
Nov 10, 2012
Slides:
51 pages
Slide Content
Slide 1
Content Providers in Android
Slide 2
●
Overall structure
●
Interaction with Content Provider
●
Constructing query
●
Retreiving cursor asyncronuously
●
Provider permissions
●
Creating Content Provider
●
Questions
Slide 3
●
Overall structure
●
Interaction with Content Provider
●
Constructing query
●
Retreiving cursor asyncronuously
●
Provider permissions
●
Creating Content Provider
●
Questions
Slide 4
Overall structure
Content Provider
Application #1
Activity #1
Activity #2
Activity #3
Database Files XML
Remote
connection
…
Activity #1
Application #2
Activity #1
Application #3
Activity #2
Slide 5
Content Provider is a source
Content Provider
Application #1
Activity #1
Activity #2
Activity #3
Database Files XML
Remote
connection
…
Activity #1
Application #2
Activity #1
Application #3
Activity #2
Slide 6
For some consumers
Content Provider
Application #1
Activity #1
Activity #2
Activity #3
Database Files XML
Remote
connection
…
Activity #1
Application #2
Activity #1
Application #3
Activity #2
Slide 7
Gives access to variety types of data
Content Provider
Application #1
Activity #1
Activity #2
Activity #3
Database Files XML
Remote
connection
…
Activity #1
Application #2
Activity #1
Application #3
Activity #2
Slide 8
Overall structure
Content Provider
Application #1
Activity #1
Activity #2
Activity #3
Database Files XML
Remote
connection
…
Activity #1
Application #2
Activity #1
Application #3
Activity #2
Slide 9
●
Overall structure
●
Interaction with Content Provider
●
Constructing query
●
Retreiving cursor asyncronuously
●
Provider permissions
●
Creating Content Provider
●
Questions
Slide 10
Interaction with Content Provider
Content Provider
Application #1
Activity #1
Activity #2
Activity #3
Database Files XML
Remote
connection
…
Activity #1
Application #2
Activity #1
Application #3
Activity #2
Slide 11
Activity to Content Provider access
Content Provider
Activity
ContentResolverCursor
CursorAdapter ListView
Slide 12
Activity
Content Provider
Activity
ContentResolverCursor
CursorAdapter ListView
Slide 13
Performing request
ContentResolver
Query
Delete
Update
Insert
Content Provider
URI
Slide 14
URI
content://com.example.provider/articles
Scheme
Authority Path
Slide 15
●
Overall structure
●
Interaction with Content Provider
●
Constructing query
●
Retreiving cursor asyncronuously
●
Provider permissions
●
Creating Content Provider
●
Questions
Slide 16
Constructing query
SELECT _id, title, content, date
FROM articles
WHERE date >= 1352470000
ORDER BY date ASC
Slide 17
Constructing query
String[] mProjection =
{
"_id",
"title",
"content",
"date",
};
String mSelection = "date >= ?";
String[] mSelectionArgs = { "1352470000"};
String mSortOrder = "date ASC";
Cursor cursor = getContentResolver().query(
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
Slide 18
Constructing query
String[] mProjection =
{
"_id",
"title",
"content",
"date",
};
String mSelection = "date >= ?";
String[] mSelectionArgs = {"1352470000"};
String mSortOrder = "date ASC";
Cursor cursor = getContentResolver().query(
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
Slide 19
Constructing query
String[] mProjection =
{
"_id",
"title",
"content",
"date",
};
String mSelection = "date >= ?";
String[] mSelectionArgs = { "1352470000"};
String mSortOrder = "date ASC";
Cursor cursor = getContentResolver().query(
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
Slide 20
Constructing query
String[] mProjection =
{
"_id",
"title",
"content",
"date",
};
String mSelection = "date >= ?";
String[] mSelectionArgs = {"1352470000"};
String mSortOrder = "date ASC";
Cursor cursor = getContentResolver().query(
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
Slide 21
Constructing query
String[] mProjection =
{
"_id",
"title",
"content",
"date",
};
String mSelection = "date >= ?";
String[] mSelectionArgs = {"1352470000"};
String mSortOrder = "date ASC";
Cursor cursor = getContentResolver().query (
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
Slide 22
Constructing query
String[] mProjection =
{
"_id",
"title",
"content",
"date",
};
String mSelection = "date >= ?";
String[] mSelectionArgs = {"1352470000"};
String mSortOrder = "date ASC";
Cursor cursor = getContentResolver().query(
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
Slide 23
Cursor
_id title content date
1 First article Lorem ipsum... 1352475013
2 Second article Dolor sit amet... 1352471413
... ... ... ...
if (mCursor != null) {
while (mCursor.moveToNext()) {
String title = mCursor.getString(Columns.TITLE);
}
}
Slide 24
●
Overall structure
●
Interaction with Content Provider
●
Constructing query
●
Retreiving cursor asyncronuously
●
Provider permissions
●
Creating Content Provider
●
Questions
Slide 25
Activity & blocking queries
Content Provider
Activity
ContentResolverCursor
CursorAdapter ListView
Slide 26
Activity & non blocking queries
Content Provider
Activity
ContentResolverCursor
CursorAdapter ListView
CursorLoader
AsyncQueryHandler
Slide 27
Activity & Loader
Content Provider
Activity
ContentResolverCursor
CursorAdapter ListView
CursorLoader
AsyncQueryHandler
Slide 28
Activity & Loader
getSupportLoaderManager().initLoader(0, null, this);
...
public class ArticlesActivity extends FragmentActivity implements
LoaderCallbacks<Cursor> {
...
public Loader<Cursor> onCreateLoader( int id, Bundle args) {
return new CursorLoader(
this, // context
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
}
public void onLoadFinished(
Loader<Cursor> loader,
Cursor cursor) {
mAdapter.swapCursor(cursor);
}
Slide 29
Activity & Loader
getSupportLoaderManager().initLoader(0, null, this);
...
public class ArticlesActivity extends FragmentActivity implements
LoaderCallbacks<Cursor> {
...
public Loader<Cursor> onCreateLoader( int id, Bundle args) {
return new CursorLoader(
this, // context
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
}
public void onLoadFinished(
Loader<Cursor> loader,
Cursor cursor) {
mAdapter.swapCursor(cursor);
}
Slide 30
Activity & Loader
getSupportLoaderManager().initLoader(0, null, this);
...
public class ArticlesActivity extends FragmentActivity implements
LoaderCallbacks<Cursor> {
...
public Loader<Cursor> onCreateLoader( int id, Bundle args) {
return new CursorLoader(
this, // context
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
}
public void onLoadFinished(
Loader<Cursor> loader,
Cursor cursor) {
mAdapter.swapCursor(cursor);
}
Slide 31
Activity & Loader
getSupportLoaderManager().initLoader(0, null, this);
...
public class ArticlesActivity extends FragmentActivity implements
LoaderCallbacks<Cursor> {
...
public Loader<Cursor> onCreateLoader( int id, Bundle args) {
return new CursorLoader(
this, // context
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
}
public void onLoadFinished(
Loader<Cursor> loader,
Cursor cursor) {
mAdapter.swapCursor(cursor);
}
Slide 32
Activity & Loader
getSupportLoaderManager().initLoader(0, null, this);
...
public class ArticlesActivity extends FragmentActivity implements
LoaderCallbacks<Cursor> {
...
public Loader<Cursor> onCreateLoader( int id, Bundle args) {
return new CursorLoader(
this, // context
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
}
public void onLoadFinished(
Loader<Cursor> loader,
Cursor cursor) {
mAdapter.swapCursor(cursor);
}
Slide 33
Activity & AsyncQueryHandler
Content Provider
Activity
ContentResolverCursor
CursorAdapter ListView
CursorLoader
AsyncQueryHandler
Slide 34
Activity & AsyncQueryHandler
private AsyncQueryHandler mHandler;
...
mHandler = new MyAsyncQueryHandler(getContentResolver());
mHandler.startQuery(
0, // token
null, // cookie
MyContentProvider.ARTICLES_CONTENT_URI ,
mProjection,
mSelection,
mSelectionArgs,
mSortOrder);
Slide 35
Activity & AsyncQueryHandler
class MyAsyncQueryHandler extends AsyncQueryHandler {
public MyAsyncQueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(
int token,
Object cookie,
Cursor cursor) {
mAdapter.swapCursor(cursor);
}
}
Slide 36
●
Overall structure
●
Interaction with Content Provider
●
Constructing query
●
Retreiving cursor asyncronuously
●
Provider permissions
●
Creating Content Provider
●
Questions
Slide 37
Permissions
<permission
android:name="com.example.provider.permission.READ_ARTICLES"
android:protectionLevel ="normal" />
<provider android:name= ".MyContentProvider"
android:exported="false"
android:authorities= "com.example.provider"
android:permission="com.example.provider.permission.READ_ARTICLES"
/>
<uses-permission
android:name="com.example.provider.permission.READ_ARTICLES" />
Slide 38
Permissions
<permission
android:name="com.example.provider.permission.READ_ARTICLES"
android:protectionLevel= "normal" />
<provider android:name=".MyContentProvider"
android:exported="false"
android:authorities="com.example.provider"
android:permission="com.example.provider.permission.READ_ARTICLES"
/>
<uses-permission
android:name="com.example.provider.permission.READ_ARTICLES" />
Slide 39
Permissions
<permission
android:name="com.example.provider.permission.READ_ARTICLES"
android:protectionLevel= "normal" />
<provider android:name= ".MyContentProvider"
android:exported="false"
android:authorities= "com.example.provider"
android:permission="com.example.provider.permission.READ_ARTICLES"
/>
<uses-permission
android:name="com.example.provider.permission.READ_ARTICLES" />
Slide 40
●
Overall structure
●
Interaction with Content Provider
●
Constructing query
●
Retreiving cursor asyncronuously
●
Provider permissions
●
Creating Content Provider
●
Questions
Slide 41
Creating content provider
public class MyContentProvider extends ContentProvider {
...
onCreate()
query()
insert()
update()
delete()
getType()
Slide 42
URI matching
content://com.example.provider/articles
content://com.example.provider/articles/*
content://com.example.provider/articles/#
Slide 43
URI matching
content://com.example.provider/articles
content://com.example.provider/articles/*
content://com.example.provider/articles/#
Slide 44
URI matching
content://com.example.provider/articles
content://com.example.provider/articles/*
content://com.example.provider/articles/#
Slide 45
URI matching
sUriMatcher.addURI("com.example.provider" , "articles/#", 0);
sUriMatcher.addURI("com.example.provider" , "articles/today", 1);
sUriMatcher.addURI("com.example.provider" , "articles/history/*" , 2);
...
public String getType(Uri uri) {
switch (sUriMatcher.match(uri)) {
...
Slide 46
URI matching
sUriMatcher.addURI("com.example.provider", "articles/#", 0);
sUriMatcher.addURI("com.example.provider", "articles/today", 1);
sUriMatcher.addURI("com.example.provider", "articles/history/*", 2);
...
public String getType(Uri uri) {
switch (sUriMatcher.match(uri)) {
...
Slide 47
MIME types
vnd.android.cursor.dir/vnd.com.example.provider .article
vnd.android.cursor.item/vnd.com.example.provider .article
getType()
getStreamTypes()
{ "image/jpeg", "image/png", "image/gif" }
Slide 48
MIME types
vnd.android.cursor.dir/vnd.com.example.provider.article
vnd.android.cursor.item/vnd.com.example.provider.article
getType()
getStreamTypes()
{ "image/jpeg", "image/png", "image/gif" }
Slide 49
Questions
Thank you!
Slide 50
Useful links
http://developer.android.com/guide/topics/providers/content-provider-basics.html
http://developer.android.com/guide/topics/providers/content-provider-creating.html
http://developer.android.com/guide/topics/security/permissions.html
http://gdg.org.ua
http://dnipro.gdg.org.ua
Slide 51
About speaker
Alexey Ustenko — Android developer
Coordniator of GDG Dnipropetrovs'k
@ustav
Tags
android
Categories
Business
Download
Download Slideshow
Get the original presentation file
Quick Actions
Embed
Share
Save
Print
Full
Report
Statistics
Views
4,709
Slides
51
Favorites
8
Age
4779 days
Related Slideshows
1
DTI BPI Pivot Small Business - BUSINESS START UP PLAN
MeljunCortes
36 views
1
CATHOLIC EDUCATIONAL Corporate Responsibilities
MeljunCortes
38 views
11
Karin Schaupp – Evocation; lançamento: 2000
alfeuRIO
36 views
10
Pillars of Biblical Oneness in the Book of Acts
JanParon
32 views
31
7-10. STP + Branding and Product & Services Strategies.pptx
itsyash298
33 views
44
Business Legislation PPT - UNIT 1 jimllpkggg
slogeshk98
36 views
View More in This Category
Embed Slideshow
Dimensions
Width (px)
Height (px)
Start Page
Which slide to start from (1-51)
Options
Auto-play slides
Show controls
Embed Code
Copy Code
Share Slideshow
Share on Social Media
Share on Facebook
Share on Twitter
Share on LinkedIn
Share via Email
Or copy link
Copy
Report Content
Reason for reporting
*
Select a reason...
Inappropriate content
Copyright violation
Spam or misleading
Offensive or hateful
Privacy violation
Other
Slide number
Leave blank if it applies to the entire slideshow
Additional details
*
Help us understand the problem better