content providers

Upload: david-vanegas

Post on 08-Jan-2016

213 views

Category:

Documents


0 download

DESCRIPTION

Material Android

TRANSCRIPT

  • Instructor: Rodrigo Ivn Fonseca Daza

  • http://www.grokkingandroid.com/android-tutorial-content-provider-basics/

  • Mecanismo para obtener datos de otras aplicaciones

    Generalmente, estos datos se encuentran almacenados en archivos sencillos, o bases de datos SQLite

  • Browser

    CalendarContract

    CallLog

    Contacts (antiguo)

    ContactsContract

    MediaStore

    Settings

    UserDictionary

  • Uniform Resource Identifier

    Para tener acceso a los datos de un ContentProvider, se necesita especificar un URI, que tiene el siguiente formato: :////

    prefix: siempre se deja como content://

    authority: nombre nico para el content provider (generalmente se llaman como los paquetes de los proyectos Java)

    data_type: tipo de dato requerido

    id: Identificador nico del registro requerido

  • Se utiliza un ContentResolver

    El ContentResolver decide qu ContentProvider se debe consultar, basado en el authority especificado en la URI

    Se puede obtener el ContentResolver desde un Activity, con el mtodo getContentResolver()

  • delete

    insert

    query

    update

  • uri

    projection

    selection

    selectionArgs

    sortOrder

  • ContentResolver resolver = getContentResolver();

    String[] projection = new String[]{BaseColumns._ID,

    UserDictionary.Words.WORD};

    Cursor cursor =

    resolver.query(UserDictionary.Words.CONTENT_URI,

    projection, null, null, null);

    if (cursor.moveToFirst()) {

    do {

    long id = cursor.getLong(0);

    String word = cursor.getString(1);

    // do something meaningful }

    while (cursor.moveToNext());

    }

  • uri

    values

  • ContentValues values = new

    ContentValues();

    values.put(Words.WORD, "Beeblebrox");

    resolver.insert(UserDictionary.Words.C

    ONTENT_URI, values);

  • uri

    values

    selection

    selectionArgs

  • values.clear();

    values.put(Words.WORD, "Zaphod");

    Uri uri =

    ContentUris.withAppendedId(Words.CONTE

    NT_URI, id);

    long noUpdated = resolver.update(uri,

    values, null, null);

  • uri

    selection

    selectionArgs

  • long noDeleted = resolver.delete

    (Words.CONTENT_URI, Words.WORD + " = ?

    ", new String[]{"Zaphod"});

  • Creacin de una clase que extienda ContentProvider

    Creacin de una clase de contrato

    Crear la definicin de un UriMatcher

    Implementar el mtodo onCreate

    Implementar el mtodo getType

    Implementar el CRUD

    Aadir el ContentProvider al Manifest

  • onCreate

    getType

    delete

    insert

    query

    update

  • Una clase de contrato sirve para establecer ciertos parmetros comunes del ContentProvider para todos aquellos que lo consulten Ej: nombre del authority, columnas a consultar por

    defecto, etc

  • Facilita la interpretacin de los URI, para determinar el tipo de dato solicitado en el ContentProvider

  • // helper constants for use with the UriMatcher private

    static final int ITEM_LIST = 1;

    private static final int ITEM_ID = 2;

    private static final int PHOTO_LIST = 5;

    private static final int PHOTO_ID = 6;

    private static final int ENTITY_LIST = 10;

    private static final int ENTITY_ID = 11; private static

  • final UriMatcher URI_MATCHER; // prepare the UriMatcher static { URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); URI_MATCHER.addURI(LentItemsContract.AUTHORITY, "items", ITEM_LIST); URI_MATCHER.addURI(LentItemsContract.AUTHORITY, "items/#", ITEM_ID); URI_MATCHER.addURI(LentItemsContract.AUTHORITY, "photos", PHOTO_LIST); URI_MATCHER.addURI(LentItemsContract.AUTHORITY, "photos/#", PHOTO_ID); URI_MATCHER.addURI(LentItemsContract.AUTHORITY, "entities", ENTITY_LIST); URI_MATCHER.addURI(LentItemsContract.AUTHORITY, "entities/#", ENTITY_ID); }

  • Creacin del ContentProvider

    Normalmente, se inicializa la base de datos

  • @Override public String getType(Uri uri) {

    switch (URI_MATCHER.match(uri)) {

    case ITEM_LIST: return Items.CONTENT_TYPE;

    case ITEM_ID: return Items.CONTENT_ITEM_TYPE; case PHOTO_ID: return Photos.CONTENT_PHOTO_TYPE;

    case PHOTO_LIST: return Photos.CONTENT_TYPE; case ENTITY_ID: return ItemEntities.CONTENT_ENTITY_TYPE;

    case ENTITY_LIST: return ItemEntities.CONTENT_TYPE;

    default: throw new IllegalArgumentException("Unsupported URI: " + uri); } }

  • query(), delete(), update(), insert()

    El ContentProvider especifica un lugar donde se guardan los datos (SQLite)

    Los mtodos mencionados deben especificar cmo se manipulan dichos datos