android下内容提供者
2015-07-02 10:01
387 查看
首先在清单文件中定义
声明内容提供者 类要继承ContentProvider
其他程序使用内容提供者 首先要声明定义内容提供者时候哪两个权限
查询所有
<provider android:name="com.example.sqllitedemp.provider.contentprovider" 提供者名称 android:authorities="com.example.contentprovider.contentprovider" android:exported="true" android:readPermission="aa.bb.cc.read" //别人要想使用 读取 必需声明这个权限 android:writePermission="aa.bb.cc.write" >//写权限 </provider>
</pre><pre name="code" class="java"> <permission android:name="aa.bb.cc.read" >//这也是在清单文件里先声明这两个权限 </permission> <permission android:name="aa.bb.cc.write" > </permission>
声明内容提供者 类要继承ContentProvider
private static final int PERSON_INSERT_CODE = 0; private static final int PERSON_UPDATE_CODE = 1; private static final int PERSON_DETELE_CODE = 2; private static final int PERSON_QUERYALL_CODE = 3; private static final int PERSON_QUERYITEM_CODE = 4; private static UriMatcher urimatcher; private static SqlLiteOperhelper Operhelper; private static String authority = "com.example.contentprovider.contentprovider"; static { urimatcher = new UriMatcher(UriMatcher.NO_MATCH); // 添加uri(分机号) // content://com.example.contentprovider.contentprovider/person/insert urimatcher.addURI(authority, "person/insert", PERSON_INSERT_CODE); // content://com.example.contentprovider.contentprovider/person/update urimatcher.addURI(authority, "person/update", PERSON_UPDATE_CODE); // content://com.example.contentprovider.contentprovider/person/detele urimatcher.addURI(authority, "person/detele", PERSON_DETELE_CODE); // content://com.example.contentprovider.contentprovider/person/queryAll urimatcher.addURI(authority, "person/queryAll", PERSON_QUERYALL_CODE); // content://com.example.contentprovider.contentprovider/person/# urimatcher.addURI(authority, "person/#", PERSON_QUERYITEM_CODE); } @Override public boolean onCreate() { Operhelper = new SqlLiteOperhelper(getContext());//获取操作sqlite数据库帮助类 return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = Operhelper.getReadableDatabase(); switch (urimatcher.match(uri)) { case PERSON_QUERYALL_CODE:// 查询全部 if (db.isOpen()) { Cursor cu = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);// return cu; // db.close();返回cursor游标结果集市不能关闭数据库 } break; case PERSON_QUERYITEM_CODE:// 查询单挑数据 url末尾有一个id if (db.isOpen()) { long id = ContentUris.parseId(uri); return db.query("person", projection, "id=?", new String[] { id + "" }, null, null, null);// } break; default: throw new IllegalArgumentException("uri不匹配" + uri); } return null; } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = Operhelper.getWritableDatabase(); switch (urimatcher.match(uri)) { case PERSON_INSERT_CODE: if (db.isOpen()) { long id = db.insert("person", null, values);// 返回新增数据的id // 通知内容观察者 内容改变 getContext() .getContentResolver() .notifyChange( Uri.parse("content://com.example.contentprovider.contentprovider"), null); db.close(); return ContentUris.withAppendedId(uri, id);//通知内容监视者 返回增加的id } break; default: throw new IllegalArgumentException("uri不匹配" + uri); } return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = Operhelper.getWritableDatabase(); switch (urimatcher.match(uri)) { case PERSON_DETELE_CODE: if (db.isOpen()) { int id = db.delete("person", selection, selectionArgs);// // 通知内容观察者 内容改变 getContext() .getContentResolver() .notifyChange( Uri.parse("content://com.example.contentprovider.contentprovider"), null); db.close(); return id; } break; default: throw new IllegalArgumentException("uri不匹配" + uri); } return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = Operhelper.getWritableDatabase(); switch (urimatcher.match(uri)) { case PERSON_UPDATE_CODE: if (db.isOpen()) { int id = db.update("person", values, selection, selectionArgs);// db.close(); return id; } break; default: throw new IllegalArgumentException("uri不匹配" + uri); } return 0; }
其他程序使用内容提供者 首先要声明定义内容提供者时候哪两个权限
查询所有
public void testqueryall() { Uri uri = Uri .parse("content://com.example.contentprovider.contentprovider/person/queryAll"); ContentResolver resolver = getContext().getContentResolver();// 内容提供者访问对象 ContentValues values = new ContentValues();//在删除和增加的时候这个可以添加参数 Cursor cu = resolver.query(uri, new String[] { "id", "name", "age" }, null, null, null); int id; String name; int age; if (cu != null && cu.getColumnCount() > 0) { while (cu.moveToNext()) { id = cu.getInt(0); name = cu.getString(1); age = cu.getInt(2); Log.i("操作", "id" + id + ",name" + name + ",age" + age); } cu.close(); } }
相关文章推荐
- Android studio第一次使用配置
- Activity与Fragment数据传递之Activity从Fragment获取数据 分类: Android 2015-07-02 09:56 12人阅读 评论(0) 收藏
- android迭代画树
- Android 自动测试_monkey测试
- Android开源项目分类
- Android支持百分比布局
- Android性能优化之电量篇
- Android 开发好习惯
- android studio 快捷键
- Android(java)学习笔记117:英文朗诵android App编写实例
- 用NDK调用Android手机自带的openssl库函数
- Android开发设计模式之——单例模式
- Android 常用RGB值以及中英文名称
- iOS 平板与 Android 平板的交互与动画
- android:ellipsize的使用
- Android DES/3DES/AES加密方式
- android:Fragment动画的东西
- android R.java文件丢失或无法更新
- Android Training: 设备管理
- Android学习 20 -> 进度条之加载进度条ProgressBar