//The following is TestBaidu
MainActivity is as follows:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* Demo description:
* Application A (TestBaidu) calls another application (TestContentProvider)
The custom ContentProvider in *, i.e.:
* 1 Customize the use of ContentProvider
* 2 Other applications call the ContentProvider
*
* Test method:
* 1 Test the contentProvider's addition, search, deletion and modification in sequence (note this order)
* 2 Other applications query the data of the ContentProvider
*
*/
public class MainActivity extends Activity {
private Button mAddButton;
private Button mDeleteButton;
private Button mUpdateButton;
private Button mQueryButton;
private Button mTypeButton;
private ContentResolver mContentResolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
init();
}
private void init() {
mContentResolver=();
mAddButton=(Button) findViewById();
(new ClickListenerImpl());
mDeleteButton=(Button) findViewById();
(new ClickListenerImpl());
mUpdateButton=(Button) findViewById();
(new ClickListenerImpl());
mQueryButton=(Button) findViewById();
(new ClickListenerImpl());
mTypeButton=(Button) findViewById();
(new ClickListenerImpl());
}
private class ClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
switch (()) {
case :
Person person=null;
for (int i = 0; i < 5; i++) {
person=new Person("xiaoming"+i, "9527"+i,(8888+i));
testInsert(person);
}
break;
case :
testDelete(1);
break;
case :
testUpdate(3);
break;
case :
//Query table
//queryFromContentProvider(-1);
//Query data with personid=2
testQuery(2);
break;
case :
testType();
break;
default:
break;
}
}
}
private void testInsert(Person person) {
ContentValues contentValues=new ContentValues();
("name", ());
("phone", ());
("salary",());
Uri insertUri=("content:///person");
Uri returnUri=(insertUri, contentValues);
("New data: returnUri="+returnUri);
}
private void testDelete(int index){
Uri uri=("content:///person/"+(index));
(uri, null, null);
}
private void testUpdate(int index){
Uri uri=("content:///person/"+(index));
ContentValues values=new ContentValues();
("name", "hanmeimei");
("phone", "1234");
("salary", 333);
(uri, values, null, null);
}
private void testQuery(int index) {
Uri uri=null;
if (index<=0) {
//Query table
uri=("content:///person");
} else {
//Check a piece of data according to id
uri=("content:///person/"+(index));
}
//Complied with the above: query table
//Cursor cursor= (uri, null, null, null, null);
//Complied with the above: query the data with personid=2
//Note: Because name is in the varchar field, it should be written as "name='xiaoming1'"
// If written as "name=xiaoming1" to query, an error will be reported
Cursor cursor= (uri, null, "name='xiaoming1'", null, null);
while(()){
int personid=(("personid"));
String name=(("name"));
String phone=(("phone"));
int salary=(("salary"));
("Query gets: person,name="+name+",phone="+phone+",salary="+salary);
}
();
}
private void testType(){
Uri dirUri=("content:///person");
String dirType=(dirUri);
("dirType:"+dirType);
Uri itemUri=("content:///person/3");
String itemType=(itemUri);
("itemType:"+itemType);
}
}
Person is as follows:
package ;
public class Person {
private Integer id;
private String name;
private String phone;
private Integer salary;
public Person(String name, String phone,Integer salary) {
= name;
= phone;
=salary;
}
public Person(Integer id, String name, String phone,Integer salary) {
= id;
= name;
= phone;
=salary;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
= id;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
= phone;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
= salary;
}
@Override
public String toString() {
return "Person [, name=" + name + ", phone=" + phone+ ", salary=" + salary + "]";
}
}
as follows:
<RelativeLayout xmlns:andro
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:text="add"
android:textSize="20sp" />
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:layout_below="@id/addButton"
android:text="delete"
android:textSize="20sp" />
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:layout_below="@id/deleteButton"
android:text="Modify"
android:textSize="20sp" />
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:layout_below="@id/updateButton"
android:text="query"
android:textSize="20sp" />
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:layout_below="@id/queryButton"
android:text="type"
android:textSize="20sp" />
</RelativeLayout>
//The following is TestContentProvider
MainActivity is as follows:
package ;
import ;
import ;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
}
}
ContentProviderTest is as follows:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* Notes:
* Properties when registering ContentProvider in
* android:exported="true" means that other applications are allowed to access.
* Only in this way can the TestBaidu app access the ContentProvider
*/
public class ContentProviderTest extends ContentProvider {
private DBOpenHelper dbOpenHelper;
private UriMatcher URI_MATCHER;
private static final int PERSONS = 0;
private static final int PERSON = 1;
@Override
public boolean onCreate() {
initUriMatcher();
dbOpenHelper=new DBOpenHelper(getContext());
return true;
}
//Initialize UriMatcher
private void initUriMatcher(){
URI_MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
// means that all persons are returned, where PERSONS is the identification code of the specific Uri
URI_MATCHER.addURI("","person", PERSONS);
// means to return a person, where PERSON is the identification code of the specific Uri
URI_MATCHER.addURI("","person/#", PERSON);
}
/**
* Insert operation:
* There is only one possibility for inserting: insert into a table
* The result is the Uri corresponding to the new record
* Method() returns the result as the primary key value corresponding to the new record
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db=();
switch (URI_MATCHER.match(uri)) {
case PERSONS:
long rowid=("person", "name,phone,salary", values);
return (uri, rowid);
default:
throw new IllegalArgumentException("unknown uri"+());
}
}
/**
* Update operation:
* There are two possibilities for updating: update a table or update a certain piece of data
* When updating a certain piece of data, the principle is similar to querying a certain piece of data, see below.
*/
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db=();
int updataNum=0;
switch (URI_MATCHER.match(uri)) {
//Update table
case PERSONS:
updataNum=("person", values, selection, selectionArgs);
break;
//Update a piece of data according to id
case PERSON:
long id=(uri);
String where="person".equals(())){
where=selection+" and "+where;
}
updataNum=("person", values, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("unknown uri"+());
}
return updataNum;
}
/**
* Delete operation:
* There are two possibilities for deletion: delete a table or delete a certain piece of data
* When deleting a certain piece of data, the principle is similar to querying a certain piece of data, see below.
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db=();
int deletedNum=0;
switch (URI_MATCHER.match(uri)) {
//Delete the table
case PERSONS:
deletedNum=("person", selection, selectionArgs);
break;
//Delete a piece of data according to id
case PERSON:
long id=(uri);
String where="person".equals(())){
where=selection+" and "+where;
}
deletedNum=("person", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("unknown uri"+());
}
return deletedNum;
}
/**
* Query operation:
* There are two possibilities for querying: querying a table or querying a certain piece of data
* Notes:
* Be careful when querying a certain piece of data - because here is querying according to personid
* A certain piece of data, but there may be other restrictions. For example:
* Require personid is 2 and name is xiaoming1
* So there are two steps when querying:
* Step 1:
* parse out the personid and put it in the where query conditions
* Part 2:
* Determine whether there are other restrictions (such as name), if so, please place them
* Group to where query conditions.
* See below for detailed code.
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db=();
Cursor cursor;
switch (URI_MATCHER.match(uri)) {
//Query table
case PERSONS:
cursor=("person", projection, selection, selectionArgs, null, null, sortOrder);
break;
//Check a piece of data according to id
case PERSON:
//first step:
long id=(uri);
String where="person".equals(())){
where=selection+" and "+where;
}
cursor=("person", projection, where, selectionArgs, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("unknown uri"+());
}
return cursor;
}
/**
* Returns the MIME type of data represented by the current Uri.
* If the data corresponding to this Uri may contain multiple records, then return
* The string should start with "/"
* If the data corresponding to this Uri only contains one record, then return
* The string should start with "/"
*/
@Override
public String getType(Uri uri) {
switch (URI_MATCHER.match(uri)) {
case PERSONS:
return "/persons";
case PERSON:
return "/person";
default:
throw new IllegalArgumentException("unknown uri"+());
}
}
}
DBOpenHelper is as follows:
package ;
import ;
import ;
import ;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context) {
super(context, "", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
("create table person(personid integer primary key autoincrement,name varchar(20),phone varchar(12),salary Integer(12))");
}
//Call this method when the database version number changes
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
//("ALTER TABLE person ADD phone varchar(12) NULL");
//("ALTER TABLE person ADD salary Integer NULL");
}
}
as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
package=""
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="" />
<uses-permission android:name=".ACCESS_NETWORK_STATE" />
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name=".MOUNT_UNMOUNT_FILESYSTEMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=""
android:label="@string/app_name" >
<intent-filter>
<action android:name="" />
<category android:name="" />
</intent-filter>
</activity>
<provider
android:name=""
android:authorities=""
android:exported="true"
/>
</application>
</manifest>
MainActivity is as follows:
Copy the codeThe code is as follows:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* Demo description:
* Application A (TestBaidu) calls another application (TestContentProvider)
The custom ContentProvider in *, i.e.:
* 1 Customize the use of ContentProvider
* 2 Other applications call the ContentProvider
*
* Test method:
* 1 Test the contentProvider's addition, search, deletion and modification in sequence (note this order)
* 2 Other applications query the data of the ContentProvider
*
*/
public class MainActivity extends Activity {
private Button mAddButton;
private Button mDeleteButton;
private Button mUpdateButton;
private Button mQueryButton;
private Button mTypeButton;
private ContentResolver mContentResolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
init();
}
private void init() {
mContentResolver=();
mAddButton=(Button) findViewById();
(new ClickListenerImpl());
mDeleteButton=(Button) findViewById();
(new ClickListenerImpl());
mUpdateButton=(Button) findViewById();
(new ClickListenerImpl());
mQueryButton=(Button) findViewById();
(new ClickListenerImpl());
mTypeButton=(Button) findViewById();
(new ClickListenerImpl());
}
private class ClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
switch (()) {
case :
Person person=null;
for (int i = 0; i < 5; i++) {
person=new Person("xiaoming"+i, "9527"+i,(8888+i));
testInsert(person);
}
break;
case :
testDelete(1);
break;
case :
testUpdate(3);
break;
case :
//Query table
//queryFromContentProvider(-1);
//Query data with personid=2
testQuery(2);
break;
case :
testType();
break;
default:
break;
}
}
}
private void testInsert(Person person) {
ContentValues contentValues=new ContentValues();
("name", ());
("phone", ());
("salary",());
Uri insertUri=("content:///person");
Uri returnUri=(insertUri, contentValues);
("New data: returnUri="+returnUri);
}
private void testDelete(int index){
Uri uri=("content:///person/"+(index));
(uri, null, null);
}
private void testUpdate(int index){
Uri uri=("content:///person/"+(index));
ContentValues values=new ContentValues();
("name", "hanmeimei");
("phone", "1234");
("salary", 333);
(uri, values, null, null);
}
private void testQuery(int index) {
Uri uri=null;
if (index<=0) {
//Query table
uri=("content:///person");
} else {
//Check a piece of data according to id
uri=("content:///person/"+(index));
}
//Complied with the above: query table
//Cursor cursor= (uri, null, null, null, null);
//Complied with the above: query the data with personid=2
//Note: Because name is in the varchar field, it should be written as "name='xiaoming1'"
// If written as "name=xiaoming1" to query, an error will be reported
Cursor cursor= (uri, null, "name='xiaoming1'", null, null);
while(()){
int personid=(("personid"));
String name=(("name"));
String phone=(("phone"));
int salary=(("salary"));
("Query gets: person,name="+name+",phone="+phone+",salary="+salary);
}
();
}
private void testType(){
Uri dirUri=("content:///person");
String dirType=(dirUri);
("dirType:"+dirType);
Uri itemUri=("content:///person/3");
String itemType=(itemUri);
("itemType:"+itemType);
}
}
Person is as follows:
Copy the codeThe code is as follows:
package ;
public class Person {
private Integer id;
private String name;
private String phone;
private Integer salary;
public Person(String name, String phone,Integer salary) {
= name;
= phone;
=salary;
}
public Person(Integer id, String name, String phone,Integer salary) {
= id;
= name;
= phone;
=salary;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
= id;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
= phone;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
= salary;
}
@Override
public String toString() {
return "Person [, name=" + name + ", phone=" + phone+ ", salary=" + salary + "]";
}
}
as follows:
Copy the codeThe code is as follows:
<RelativeLayout xmlns:andro
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:text="add"
android:textSize="20sp" />
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:layout_below="@id/addButton"
android:text="delete"
android:textSize="20sp" />
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:layout_below="@id/deleteButton"
android:text="Modify"
android:textSize="20sp" />
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:layout_below="@id/updateButton"
android:text="query"
android:textSize="20sp" />
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:layout_below="@id/queryButton"
android:text="type"
android:textSize="20sp" />
</RelativeLayout>
//The following is TestContentProvider
MainActivity is as follows:
Copy the codeThe code is as follows:
package ;
import ;
import ;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
}
}
ContentProviderTest is as follows:
Copy the codeThe code is as follows:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* Notes:
* Properties when registering ContentProvider in
* android:exported="true" means that other applications are allowed to access.
* Only in this way can the TestBaidu app access the ContentProvider
*/
public class ContentProviderTest extends ContentProvider {
private DBOpenHelper dbOpenHelper;
private UriMatcher URI_MATCHER;
private static final int PERSONS = 0;
private static final int PERSON = 1;
@Override
public boolean onCreate() {
initUriMatcher();
dbOpenHelper=new DBOpenHelper(getContext());
return true;
}
//Initialize UriMatcher
private void initUriMatcher(){
URI_MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
// means that all persons are returned, where PERSONS is the identification code of the specific Uri
URI_MATCHER.addURI("","person", PERSONS);
// means to return a person, where PERSON is the identification code of the specific Uri
URI_MATCHER.addURI("","person/#", PERSON);
}
/**
* Insert operation:
* There is only one possibility for inserting: insert into a table
* The result is the Uri corresponding to the new record
* Method() returns the result as the primary key value corresponding to the new record
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db=();
switch (URI_MATCHER.match(uri)) {
case PERSONS:
long rowid=("person", "name,phone,salary", values);
return (uri, rowid);
default:
throw new IllegalArgumentException("unknown uri"+());
}
}
/**
* Update operation:
* There are two possibilities for updating: update a table or update a certain piece of data
* When updating a certain piece of data, the principle is similar to querying a certain piece of data, see below.
*/
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db=();
int updataNum=0;
switch (URI_MATCHER.match(uri)) {
//Update table
case PERSONS:
updataNum=("person", values, selection, selectionArgs);
break;
//Update a piece of data according to id
case PERSON:
long id=(uri);
String where="person".equals(())){
where=selection+" and "+where;
}
updataNum=("person", values, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("unknown uri"+());
}
return updataNum;
}
/**
* Delete operation:
* There are two possibilities for deletion: delete a table or delete a certain piece of data
* When deleting a certain piece of data, the principle is similar to querying a certain piece of data, see below.
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db=();
int deletedNum=0;
switch (URI_MATCHER.match(uri)) {
//Delete the table
case PERSONS:
deletedNum=("person", selection, selectionArgs);
break;
//Delete a piece of data according to id
case PERSON:
long id=(uri);
String where="person".equals(())){
where=selection+" and "+where;
}
deletedNum=("person", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("unknown uri"+());
}
return deletedNum;
}
/**
* Query operation:
* There are two possibilities for querying: querying a table or querying a certain piece of data
* Notes:
* Be careful when querying a certain piece of data - because here is querying according to personid
* A certain piece of data, but there may be other restrictions. For example:
* Require personid is 2 and name is xiaoming1
* So there are two steps when querying:
* Step 1:
* parse out the personid and put it in the where query conditions
* Part 2:
* Determine whether there are other restrictions (such as name), if so, please place them
* Group to where query conditions.
* See below for detailed code.
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db=();
Cursor cursor;
switch (URI_MATCHER.match(uri)) {
//Query table
case PERSONS:
cursor=("person", projection, selection, selectionArgs, null, null, sortOrder);
break;
//Check a piece of data according to id
case PERSON:
//first step:
long id=(uri);
String where="person".equals(())){
where=selection+" and "+where;
}
cursor=("person", projection, where, selectionArgs, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("unknown uri"+());
}
return cursor;
}
/**
* Returns the MIME type of data represented by the current Uri.
* If the data corresponding to this Uri may contain multiple records, then return
* The string should start with "/"
* If the data corresponding to this Uri only contains one record, then return
* The string should start with "/"
*/
@Override
public String getType(Uri uri) {
switch (URI_MATCHER.match(uri)) {
case PERSONS:
return "/persons";
case PERSON:
return "/person";
default:
throw new IllegalArgumentException("unknown uri"+());
}
}
}
DBOpenHelper is as follows:
Copy the codeThe code is as follows:
package ;
import ;
import ;
import ;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context) {
super(context, "", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
("create table person(personid integer primary key autoincrement,name varchar(20),phone varchar(12),salary Integer(12))");
}
//Call this method when the database version number changes
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
//("ALTER TABLE person ADD phone varchar(12) NULL");
//("ALTER TABLE person ADD salary Integer NULL");
}
}
as follows:
Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
package=""
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="" />
<uses-permission android:name=".ACCESS_NETWORK_STATE" />
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name=".MOUNT_UNMOUNT_FILESYSTEMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=""
android:label="@string/app_name" >
<intent-filter>
<action android:name="" />
<category android:name="" />
</intent-filter>
</activity>
<provider
android:name=""
android:authorities=""
android:exported="true"
/>
</application>
</manifest>