viernes, 2 de enero de 2015

Content Providers on Android APPS using SQLiteDatabase

CONTENT PROVIDER ANDROID

A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.
Each Android applications runs in its own process with its own permissions which keeps an application data hidden from another application. But sometimes it is required to share data across applications. This is where content providers become very useful.
Content providers let you centralize content in one place and have many different applications access it as needed. A content provider behaves very much like a database where you can query it, edit its content, as well as add or delete content usingg insert(), update(), delete(), and query() methods. In most cases this data is stored in an SQlite database. A content provider is implemented as a subclass of ContentProvider class and must implement a standard set of APIs that enable other applications to perform transactions.

Next you will need to create your own database to keep the content. Usually, Android uses SQLite database and framework needs to override onCreate() method which will use SQLite Open Helper method to create or open the provider's databse. When your application is launched, theonCreate() handler of each of its Content Providers is called on the main application thread.

Here is the list of methods which you need to override in Content Provider class to have your Content Provider working:
 onCreate() This method is called when the provider is started. 
 query() This method receives a request from a client. The result is returned as a Cursor object. 
 insert()This method inserts a new record into the content provider. 
 delete() This method deletes an existing record from the content provider. 
 update() This method updates an existing record from the content provider. 
 getType() This method returns the MIME type of the data at the given URI.

We need to edit the android manifesto as follows:



Modify the MainActivity.java class with the folowing code:
a)

b)


Then we need to create the class studentsProvider in the same package of MainActivity:
a)
b)

We need to add the activity main configuration:

IN RESUMEN the following steps are implemented:
Description
You will use Eclipse IDE to create an Android application and name it as MyContentProviderunder a package com.example.mycontentprovider, with blank Activity.
Modify main activity file MainActivity.java to add two new methods onClickAddName() andonClickRetrieveStudents().
Create a new java file called StudentsProvider.java under the packagecom.example.mycontentprovider to define your actual provider and associated methods.
Register your content provider in your AndroidManifest.xml file using <provider.../> tag
Modify the detault content of res/layout/activity_main.xml file to include a small GUI to add sudents records.
Define required constants in res/values/strings.xml file
Run the application to launch Android emulator and verify the result of the changes done in the aplication.


ELRESULTADO DEL APP ES EL SIGUIENTE:









No hay comentarios:

Publicar un comentario

Blogger Widgets