Android四大组件之Content Provider(内容提供者)
2015-10-27 22:46
846 查看
Content Provider简介
1、什么是Content Provider
所谓内容提供者官方给出的概念是:为存储和获取数据提供统一的接口。可以在不同的应用程序之间共享数据。2、什么时候使用
一般情况下我们知道自己应用程序的数据库是私有的,别的应用是不能进行访问的,可是有时我们需要把应用的数据库暴露给其他的应用程序进行增删改查,比如一些备份手机联系人的应用,这时就可以用到了Content Provider。3、怎么用
(1)编写一个类继承ContentProvider,实现里面增删改查的方法(2)在清单文件中配置内容提供者,注意指定android:authorities="com.xxx.db"(这个是口令,如果那个应用想用ContentProvider需要匹配口令,为了可读性强,一般用所在项目的包名来命名)
(3)在内容提供者的类的内部声明UriMatcher,用来为别的应用匹配口令
(4)为mUriMatcher添加需要检测的uri,调用mUriMatcher.addURI("com.itheima.db", "account", SUCCESS);(uri就是上面"com.xxx.db",第二个参数是为了精确匹配又加的一个“暗语”,第三个参数是判断匹配成功的标示)。
(5)调用mUriMatcher.match(uri)来检查uri路径是否正确
(6)在另外一个应用程序里面 通过contentResolver 增删改查
4、示例代码
内容提供者代码:package com.itheima.db; 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; /** * 内容提供者 后门程序,提供私有的数据给别的应用程序,默认都是空实现. */ public class BankDBBackdoor extends ContentProvider { public static final int SUCCESS = 1; /** * 创建一个保安,检查uri的规则,如果uri匹配失败 返回-1 */ static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static { mUriMatcher.addURI("com.itheima.db", "account", SUCCESS); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int code = mUriMatcher.match(uri); if (code == SUCCESS) { System.out.println("delete 删除数据"); MyDBOpenHelper helper = new MyDBOpenHelper(getContext()); SQLiteDatabase db = helper.getWritableDatabase(); db.delete("account",selection , selectionArgs); //利用内容提供者的解析器,通知内容观察者数据发生了变化 getContext().getContentResolver().notifyChange(uri, null); }else{ throw new IllegalArgumentException("口令 不正确,滚犊子"); } return 0; } @Override public String getType(Uri uri) { return null; } @Override public Uri insert(Uri uri, ContentValues values) { int code = mUriMatcher.match(uri); if (code == SUCCESS) { System.out.println("insert 添加数据"); MyDBOpenHelper helper = new MyDBOpenHelper(getContext()); SQLiteDatabase db = helper.getWritableDatabase(); db.insert("account", null, values); //利用内容提供者的解析器,通知内容观察者数据发生了变化 getContext().getContentResolver().notifyChange(uri, null); }else{ throw new IllegalArgumentException("口令 不正确,滚犊子"); } return null; } @Override public boolean onCreate() { return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { int code = mUriMatcher.match(uri); if (code == SUCCESS) { System.out.println("query 查询数据"); MyDBOpenHelper helper = new MyDBOpenHelper(getContext()); SQLiteDatabase db = helper.getReadableDatabase(); return db.query("account", projection, selection, selectionArgs, null, null, sortOrder); }else{ throw new IllegalArgumentException("口令 不正确,滚犊子"); } } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int code = mUriMatcher.match(uri); if (code == SUCCESS) { System.out.println("update 更新数据"); MyDBOpenHelper helper = new MyDBOpenHelper(getContext()); SQLiteDatabase db = helper.getWritableDatabase(); db.update("account", values, selection, selectionArgs); //利用内容提供者的解析器,通知内容观察者数据发生了变化 getContext().getContentResolver().notifyChange(uri, null); }else{ throw new IllegalArgumentException("口令 不正确,滚犊子"); } return 0; } }
调用内容提供者的代码:
package com.itheima.bankboss; import java.util.Currency; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.View; /** * 使用内容提供者的类 * @author LBK * */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 利用后门 添加一条数据 */ public void insert(View view) { // 得到内容提供者的解析器 ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.itheima.db/account"); ContentValues values = new ContentValues(); values.put("name", "zhangsan"); values.put("money", 10000); // 通过内容解析器让内容提供者添加一条数据 resolver.insert(uri, values); } public void delete(View view) { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.itheima.db/account"); resolver.delete(uri, "name=?", new String[]{"zhangsan"}); } public void update(View view) { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.itheima.db/account"); ContentValues values = new ContentValues(); values.put("money", 20000); resolver.update(uri, values, "name=?", new String[]{"zhangsan"}); } public void query(View view) { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.itheima.db/account"); Cursor cursor = resolver.query(uri, new String[]{"name","money"}, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(0); float money = cursor.getFloat(1); System.out.println("name:"+name+"----"+"money:"+money); } cursor.close(); } }
相关文章推荐
- Android 中加载几百张图片做帧动画防止 OOM 的解决方案
- android自定义圆形图像
- Android存储-SharedPreferences
- 胖虎谈ImageLoader框架(一)
- 【Android】记录反编译安卓程序步骤
- Android开发之ListView异步加载图片
- Androidstudio 点9图不能识别报错的问题
- Android的自定义View---自绘控件
- android实现开机自动播放音乐实例(Broadcast、Service)
- Android:控件AutoCompleteTextView
- android复习第二天------布局
- Android学习笔记之AndroidManifest.xml文件解析
- Android Studio安装
- Android 适配
- Android 字体库的使用
- Android 字体库的使用
- Android 无线调试
- Android启动模式之lunchMode
- android studio设置debug.keystore
- ViewDragHelper详解