1. Trang chủ
  2. » Giáo án - Bài giảng

Content provider document

50 317 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 373,99 KB

Nội dung

Android Course © 2011 University of Science – HCM City . M.Sc. Bui Tan Loc btloc@fit.hcmus.edu.vn Department of Software Engineering, Faculty of Information Technology, University of Science – Ho Chi Minh City, Viet Nam Content Provider Android Course © 2011 University of Science – HCM City . Objectives • After completing this module, you will have learned: • About using Content Provider to access data within other applications • About using Content Provider to expose internal application data to other applications 2 Android Course © 2011 University of Science – HCM City . Contents • Querying records • Adding records • Updating records • Deleting records • Acting as a Content Provider 3 Android Course © 2011 University of Science – HCM City . Content Provider • Applications can access data within other applications on the Android system through content provider interfaces and expose internal application data to other applications by becoming a content provider. Android Course © 2011 University of Science – HCM City . Accessing data within other applications Android Course © 2011 University of Science – HCM City . Querying records #1 // Queries and returns results Cursor cur = managedQuery( uri, // The content URI projection, // The columns to return for each row selectionClause,// Selection criteria selectionArgs, // Selection criteria sortOrder); // The sort order for the returned rows Android Course © 2011 University of Science – HCM City . Querying records #2 // Queries and returns results ContentResolver cr = getContentResolver(); Cursor cur = cr.query( uri, // The content URI projection, // The columns to return for each row selectionClause,// Selection criteria selectionArgs, // Selection criteria sortOrder); // The sort order for the returned rows // … cur.close(); Android Course © 2011 University of Science – HCM City . Querying records #3 // Queries and returns results ContentResolver cr = getContentResolver(); Cursor cur = cr.query( uri, // The content URI projection, // The columns to return for each row selectionClause,// Selection criteria selectionArgs, // Selection criteria sortOrder); // The sort order for the returned rows startManagingCursor(cur); // … stopManagingCursor(cur); Android Course © 2011 University of Science – HCM City . Querying records query() argument SELECT keyword/parameter Notes uri FROM table_name Uri maps to the table in the provider named table_name . projection col,col,col, projection is an array of columns that should be included for each row retrieved. selection WHERE col = value selection specifies the criteria for selecting rows. selectionArgs (No exact equivalent. Selection arguments replace ? placeholders in the selection clause.) sortOrder ORDER BY col,col, sortOrder specifies the order in which rows appear in the returned Cursor. Android Course © 2011 University of Science – HCM City . Adding records ContentResolver cr = getContentResolver(); Uri url = …; ContentValues values = new ContentValues(); values.put(key1, value); values.put(key1, value); cr.insert(url, values); values.clear(); values.put(key3, value3); values.put(key4, value4); cr.insert(url, values); [...]... Science – HCM City Android Course Defining the Data URI public static final String PROVIDER_ NAME = "com.vietandroid .provider. Books"; public static final Uri CONTENT_ URI = Uri.parse( "content: //" + PROVIDER_ NAME + "/books"); Updating the mainifest file © 2011 University of Science – HCM City Android Course Defining... make an application a content provider is to store the information you want to share in a SQLite database © 2011 University of Science – HCM City Android Course BookProvider public class BookProvider extends ContentProvider { public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; } public String getType(Uri uri) { return null; } public Uri insert(Uri uri, ContentValues values)... Acting as a Content Provider • Do you have data in your application? Can another application do something interesting with that data? To share the information within your application with other applications, you need to make the application a content provider by providing the standardized con-tent provider interface for other applications; then you must register your application as a content provider. .. 2011 University of Science – HCM City Android Course Adding records ContentValues values = new ContentValues(); values.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "Simple User"); ContentResolver cr = getContentResolver(); Uri rawContactUri = cr.insert(ContactsContract.RawContacts .CONTENT_ URI, values); long rawContactId = ContentUris.parseId(rawContactUri); values.clear(); values.put(ContactsContract.Data.RAW_CONTACT_ID,... getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID)); ContentValues values = new ContentValues(); values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactID); values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note .CONTENT_ ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.Note.NOTE, "Example Note !"); ContentResolver cr = getContentResolver(); int updatedRows = cr.update(ContactsContract.Data .CONTENT_ URI,... Science – HCM City Android Course Deleting records // Delete all records ContentResolver cr = getContentResolver(); Uri personUri = ContactsContract.RawContacts .CONTENT_ URI; int deletedRows = cr.delete(personUri, null, null); // Delete specific record ContentResolver cr = getContentResolver(); Uri personUri = ContactsContract.RawContacts .CONTENT_ URI; String where =ContactsContract.Contacts.DISPLAY_NAME+"=?";... University of Science – HCM City Android Course BookProvider public class BookProvider extends ContentProvider { // delete() // getType() // insert() public boolean onCreate() { return false; } public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { return null; } public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)...Android Course Updating records // Updating records ContentResolver cr = getContentResolver(); Uri url = …; String where = …; String[] selectionArgs = {…}; int updatedRows = cr.update(url, values, where, selectionArgs); © 2011 University of Science – HCM City Android Course Deleting records // Deleting records ContentResolver cr = getContentResolver(); Uri url = …; String where = …; String[]... values.put(ContactsContract.Data.MIMETYPE, StructuredName .CONTENT_ ITEM_TYPE); values.put(StructuredName.DISPLAY_NAME, "Simple User"); cr.insert(ContactsContract.Data .CONTENT_ URI, values); © 2011 University of Science – HCM City Android Course Updating records String where =ContactsContract.Contacts.DISPLAY_NAME+"=?"; Cursor cur = managedQuery(ContactsContract.Data .CONTENT_ URI, null, where, new String[]{"Simple... ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; String[] imWhereParams = new String[] {id, ContactsContract.CommonDataKinds.Im .CONTENT_ ITEM_TYPE }; Cursor imCur = managedQuery(ContactsContract.Data .CONTENT_ URI, null, imWhere, imWhereParams, null); if (imCur.moveToFirst()) { String imName = imCur.getString(imCur getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));

Ngày đăng: 02/02/2015, 11:13

TỪ KHÓA LIÊN QUAN

w