Create a Content Provider

Source code of content provider in President.zip

  1. MainActivity.java
  2. PresidentContract.java
  3. PresidentProvider.java
  4. Helper.java
  5. activity_main.xml
  6. strings.xml
  7. AndroidManifest.xml. The <manifest> element contains two <permission> elements. The <application> element contains a <provider> element.
  8. build.gradle (Module: app)

Source code of content provider’s client in PresidentClient.zip

  1. MainActivity.java
  2. PresidentContract.java: identical to the PresidentContract.java in the content provider, except for the package statement.
  3. activity_main.xml
  4. strings.xml
  5. AndroidManifest.xml
  6. build.gradle (Module: app)

Create the content provider

To create class PresidentProvider, select the app/java/president folder in the Android Studio project view and pull down
File → New → Other → Content Provider

Run the projects

First run the content provider President, and leave it running. You can ignore its Activity. Then run PresidentCleint as many times as you want. After its first run, the content provider’s database looks like this:

adb devices
List of devices attached
192.168.57.101:5555	device

adb -s 192.168.57.101:5555 shell

cd /data/data/edu.nyu.scps.president/databases
pwd
/data/data/edu.nyu.scps.president/databases

ls -l
-rw-rw---- u0_a109  u0_a109     20480 2015-08-08 07:18 presidents.db

sqlite3 presidents.db
.tables
android_metadata  presidents

.dump android_metadata
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE android_metadata (locale TEXT);
INSERT INTO "android_metadata" VALUES('en_US');
COMMIT;

.dump presidents
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE presidents (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
INSERT INTO presidents VALUES(1,'Washington');
INSERT INTO presidents VALUES(2,'John Adams');
COMMIT;

.quit
exit
adb -s 192.168.57.101:5555 shell sqlite3 /data/data/edu.nyu.scps.president/databases/presidents.db "'.dump presidents'"
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE presidents (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
INSERT INTO presidents VALUES(1,'Washington');
INSERT INTO presidents VALUES(2,'John Adams');
COMMIT;

Content providers in ApiDemos

  1. Content → Provider → PickContact
    1. ApiDemos/src/com/example/android/apis/content/PickContact.java
    2. ApiDemos/res/pick_contact.xml
    3. ApiDemos/res/values/strings.xml

Things to try