访问其他程序中的数据
效果图
该程序访问电话簿的数据,将信息写在ListView中。
源代码
1、内容提供其的URI由 协议声明,权限和路径 组合而成比如 content://com.example.chapter6_sqltest.provider/book
2、ContentResolver通过Uri,可以进行query、insert、update和delete操作,很类似数据库。
接下来看看这个程序如何实现
首先定义布局文件,activity_main.xml
这里只定义了一个ListView,用于存放电话簿中数据。
MainActivity.java
package com.example.chapter7_contactstest;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView contactsView;
ArrayAdapter adapter;
List contactsList = new ArrayList();
/**
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactsView = (ListView)findViewById(R.id.contacts_view);
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,contactsList);
contactsView.setAdapter(adapter);
readContacts();
}
private void readContacts(){
Cursor cursor = null;
try {
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
while (cursor.moveToNext()){
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactsList.add(displayName+"\n"+number);
}
// getContentResolver().delete(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null);
}catch (Exception e){
e.printStackTrace();
}
finally {
if (cursor!=null){
cursor.close();
}
}
}
}
这里用到了系统封装的Uri, ContactsContact.CommonDataKinds.Phone.CONTENT_URI,这个Uri指向了电话簿的table,所以我们可以通过这个Uri进行查询操作了。
别忘了加上权限哦
创建自己的内容提供器
源代码
实现跨程序数据共享,创建自己的ContentProvider,需要注意几点
1、对Uri进行匹配,借助UriMatch这个类进行匹配。
2、对getType的复写,return的type形式略有不同 vnd+android.cursor.dir/(或者是android.cursor.item/,这取决于内容Uri目的)+vnd.
. 例如:
vnd.android.cursor.dir/vnd.com.example.app.provider.table1
3、对manifest进行注册
首先,我们要对上一个demo的数据库进行分享,所以我们要先修改它。在chapter6_sqltest这个工程下创建新类:
DatabaseProvider.java
package com.example.chapter6_sqltest;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
/**
* 项目名称:Chapter6_SQLTest
* 类描述:
* 创建人:吴乐
* 创建时间:2016/4/18 16:14
* 修改人:吴乐
* 修改时间:2016/4/18 16:14
* 修改备注:
*/
public class DatabaseProvider extends ContentProvider {
public static final int BOOK_DIR = 0;
public static final int BOOK_ITEM = 1;
public static final int CATEGORY_DIR = 2;
public static final int CATEGORY_ITEM = 3;
public static final String AUTHORITY = "com.example.chapter6_sqltest.provider";
private static UriMatcher uriMatcher;
private MyDatabaseHelper dbHelper;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "book", BOOK_DIR);
uriMatcher.addURI(AUTHORITY, "book/#", BOOK_ITEM);
uriMatcher.addURI(AUTHORITY, "category", CATEGORY_DIR);
uriMatcher.addURI(AUTHORITY, "category/#", CATEGORY_ITEM);
}
@Override
public boolean onCreate() {
dbHelper = new MyDatabaseHelper(getContext(), "BookStore.db", null, 2);
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = null;
switch (uriMatcher.match(uri)) {
case BOOK_DIR:
cursor = db.query("Book", projection, selection, selectionArgs, null, null, sortOrder);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
cursor = db.query("Book", projection, "id=?", new String[]{bookId}, null, null, sortOrder);
break;
case CATEGORY_DIR:
cursor = db.query("Category", projection, selection, selectionArgs, null, null, sortOrder);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
cursor = db.query("Category", projection, "id=?", new String[]{categoryId}, null, null, sortOrder);
break;
default:
break;
}
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
//添加数据
SQLiteDatabase db = dbHelper.getWritableDatabase();
Uri uriReturn = null;
switch (uriMatcher.match(uri)) {
case BOOK_DIR:
case BOOK_ITEM:
long newBookId = db.insert("Book", null, values);
uriReturn = Uri.parse("content://" + AUTHORITY + "/book/" + newBookId);
break;
case CATEGORY_DIR:
case CATEGORY_ITEM:
long newCategoryId = db.insert("Category", null, values);
uriReturn = Uri.parse("content://" + AUTHORITY + "/category/" + newCategoryId);
break;
default:
break;
}
return uriReturn;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
int updatedRows = 0;
switch (uriMatcher.match(uri)) {
case BOOK_DIR:
updatedRows = db.update("Book", values, selection, selectionArgs);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
updatedRows = db.update("Book", values, "id=?", new String[]{bookId});
break;
case CATEGORY_DIR:
updatedRows = db.update("Category", values, selection, selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
updatedRows = db.update("Category", values, "id=?", new String[]{categoryId});
break;
}
return updatedRows;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
int deleteRows = 0;
switch (uriMatcher.match(uri)) {
case BOOK_DIR:
deleteRows = db.delete("Book", selection, selectionArgs);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
deleteRows = db.delete("Book", "id=?", new String[]{bookId});
break;
case CATEGORY_DIR:
deleteRows = db.delete("Category", selection, selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
deleteRows = db.delete("Category", "id=?", new String[]{categoryId});
break;
default:
break;
}
return deleteRows;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.com.example.chapter6_sqltest.provider.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com.example.chapter6_sqltest.provider.book";
case CATEGORY_DIR:
return "vnd.android.cursor.dir/vnd.com.example.chapter6_sqltest.provider.category";
case CATEGORY_ITEM:
return "vnd.android.cursor.item/vnd.com.example.chapter6_sqltest.provider.category";
}
return null;
}
}
其次,对manifest进行修改
相对而言,添加了一个provider。这样一个跨程序的数据共享的准备工作就完成了,最后,我们再新建一个app,看能不能用它。
新建一个ProviderTest工程,修改activity_main.xml如下:
接下来是MainActivity,java
package com.example.providertest;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private String newId;
private final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addData = (Button) findViewById(R.id.add_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("content://com.example.chapter6_sqltest.provider/book");
ContentValues values = new ContentValues();
values.put("name", "A Clash of Kings");
values.put("author", "George Martin");
values.put("pages", 1024);
values.put("price", 22.85);
Uri newUri = getContentResolver().insert(uri, values);
newId = newUri.getPathSegments().get(1);
}
});
Button queryData = (Button) findViewById(R.id.query_data);
queryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("content://com.example.chapter6_sqltest.provider/book");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String auther = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
Log.d(TAG, "onClick: name-->" + name);
Log.d(TAG, "onClick: author-->" + auther);
Log.d(TAG, "onClick: pages-->" + pages);
Log.d(TAG, "onClick: price-->" + price);
}
cursor.close();//��סclose
}
}
});
Button updateData = (Button)findViewById(R.id.update_data);
updateData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("content://com.example.chapter6_sqltest.provider/book/"+newId);
ContentValues values = new ContentValues();
values.put("name","A Storm of Swords");
values.put("pages",1216);
values.put("price",24.05);
getContentResolver().update(uri,values,null,null);
}
});
Button deleteData = (Button)findViewById(R.id.delete_data);
deleteData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("content://com.example.chapter6_sqltest.provider/book/"+newId);
getContentResolver().delete(uri,null,null);
}
});
}
}
这里使用方法和上面访问电话簿很相似,当我们按下Add to book这个Button时,我们观察一下数据库中内容:
可以看见,数据被成功添加到Chapter6_sqltest工程中的数据库里了!是不是很爽!
接下来我们按下查询按钮:
可以看见,数据成功打印在我们的工程目录中。接下来我们更新数据,
可以观察到,数据被成功更新。接下来我们删除数据
此时,我们查询到任何数据。至此,我们就实现了对数据库的跨程序共享。
通过这个例子,我们认识到对数据的跨程序共享是android的重要特征之一,很多基础的数据是很有必要共享的。android自身也做了很多跨程序的共享工作,如电话簿,短信等等,这是一个有选择的共享过程,所以相对而言是安全的!
Fighting!Fighting!Fighting!