android contentprovider内容提供者
contentprovider内容提供者:让其他app可以访问私有数据库(文件)
1.AndroidManifest.xml
配置provider
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dbtest"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!--android:name="com.example.dbtest.PersonContentProvider" 必须为内容提供者类的路径 不然会报notfoundclass-->
<provider
android:name="com.example.dbtest.PersonContentProvider"
android:authorities="com.example.dbtest.provider.personprovider"
android:exported="true"></provider> <activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
2.PersonContentProvider
package com.example.dbtest; import com.example.dbtest.dbHelper.DbOpenHelper; 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 PersonContentProvider extends ContentProvider { private DbOpenHelper helper;
//定义一个uri的匹配器用于匹配uri 如果路径不满足条件 返回-1
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); private static final int INSERT = 1;
private static final int DELETE = 2;
private static final int UPDATE = 3;
private static final int QUERY = 4; static{
matcher.addURI("com.example.dbtest.provider.personprovider", "insert", INSERT);
matcher.addURI("com.example.dbtest.provider.personprovider", "delete", DELETE);
matcher.addURI("com.example.dbtest.provider.personprovider", "update", UPDATE);
matcher.addURI("com.example.dbtest.provider.personprovider", "query", QUERY);
} //content://com.itheima.db.personprovider/insert 添加操作
//content://com.itheima.db.personprovider/delete 删除操作
//content://com.itheima.db.personprovider/update 更新操作
//content://com.itheima.db.personprovider/query 查询操作 @Override
public boolean onCreate() {
helper = new DbOpenHelper(getContext());
return true;
} @Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
if(matcher.match(uri)==QUERY)
{
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
//注意 使用contentprovider时 不要将数据关闭掉 不然拿不到数据
//db.close(); 不要关闭
return cursor;
}
else
{
throw new IllegalArgumentException("路径不配对,不能执行查询操作");
}
} @Override
public String getType(Uri uri) { return null;
} @Override
public Uri insert(Uri uri, ContentValues values) {
if(matcher.match(uri)==INSERT)
{
SQLiteDatabase db = helper.getWritableDatabase();
db.insert("person",null,values);
}
else
{
throw new IllegalArgumentException("路径不配对,不能执行插入操作");
}
return null;
} @Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
if(matcher.match(uri)==DELETE)
{
SQLiteDatabase db = helper.getWritableDatabase();
db.delete("person", selection, selectionArgs);
}
else
{
throw new IllegalArgumentException("路径不配对,不能执行删除操作");
}
return 0;
} @Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
if(matcher.match(uri)==UPDATE)
{
SQLiteDatabase db = helper.getWritableDatabase();
db.update("person", values, selection, selectionArgs);
}
else
{
throw new IllegalArgumentException("路径不配对,不能执行修改操作");
}
return 0;
} }
3.其他app调用内容提供者
package com.example.getcontentprovider; import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast; public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void query(View view)
{
System.out.println("start query...............");
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://com.example.dbtest.provider.personprovider/query");
Cursor cursor = resolver.query(uri, null, null, null, null); StringBuffer sb = new StringBuffer();
while(cursor.moveToNext())
{
System.out.println("name:"+cursor.getString(cursor.getColumnIndex("name")));
sb.append("name:"+cursor.getString(cursor.getColumnIndex("name"))+"\n"); }
System.out.println("end query.............."); Toast.makeText(this, sb.toString(),0).show(); } public void delete(View view)
{
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://com.example.dbtest.provider.personprovider/delete");
resolver.delete(uri, "id=?", new String[]{"1"}); Toast.makeText(this, "删除成功",0).show(); } public void update(View view)
{
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://com.example.dbtest.provider.personprovider/update");
ContentValues values = new ContentValues();
values.put("name", "张三");
resolver.update(uri, values, "id=?", new String[]{"1"}); Toast.makeText(this, "修改成功",0).show(); } public void insert(View view)
{ ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://com.example.dbtest.provider.personprovider/insert");
ContentValues values = new ContentValues();
values.put("name", "李四");
resolver.insert(uri, values); Toast.makeText(this, "修改成功",0).show();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
android contentprovider内容提供者的更多相关文章
- Android -- ContentProvider 内容提供者,创建和调用
1. 概述 ContentProvider 在android中的作用是对外共享数据,也就是说你可以通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过ContentPr ...
- Android组件系列----ContentProvider内容提供者
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- android 53 ContentProvider内容提供者
ContentProvider内容提供者:像是一个中间件一样,一个媒介一样,可以以标准的增删改差操作对手机的文件.数据库进行增删改差.通过ContentProvider查找sd卡的音频文件,可以提供标 ...
- Android中内容提供者ContentProvider的详解
1.什么是ContentProvider 首先,ContentProvider(内容提供者)是android中的四大组件之一,但是在一般的开发中,可能使用的比较少. ContentProvider为不 ...
- Android组件系列----ContentProvider内容提供者【1】
[正文] 一.ContentProvider简单介绍: ContentProvider内容提供者(四大组件之中的一个)主要用于在不同的应用程序之间实现数据共享的功能. ContentProvider能 ...
- Android基础内容提供者ContentProvider的使用详解(转)
1.什么是ContentProvider 首先,ContentProvider(内容提供者)是android中的四大组件之一,但是在一般的开发中,可能使用的比较少. ContentProvider为不 ...
- contentProvider内容提供者
contentProvider内容提供者 15. 四 / android基础 / 没有评论 步骤 权限在application中注册 Source code <provider an ...
- Android 中内容提供者的使用
在Android中内容提供者主要是用于不同程序之间的数据共享.内容提供器的用法一般有两种,一种是使用现有的内容提供器来读取和操作相应程序的数据,另一种是创建自己的内容提供器,供其他的程序访问. 使用现 ...
- Android开发学习—— ContentProvider内容提供者
* 应用的数据库是不允许其他应用访问的* 内容提供者的作用就是让别的应用访问到你的数据库.把私有数据暴露给其他应用,通常,是把私有数据库的数据暴露给其他应用. Uri:包含一个具有一定格式的字符串的对 ...
随机推荐
- cordic
cordic里向量旋转得到新向量,利用的是旋转矩阵 摘自百度百科维基百科 旋转矩阵(Rotation matrix)是在乘以一个向量的时候改变向量的方向但不改变大小的效果的矩阵.旋转矩阵不包括反演,它 ...
- day30 item系列
item 会将数据操作类似于字典的操作具体用到的方法 __getitem__(self, item): __setitem__(self, key, value): __delitem__(self, ...
- mysql format函数对数字类型转化的坑
原值param = 1234.5678 format(param, 2) (不建议) 结果,字符串类型,123,4.57 会导致你图表char 生成失败,直接变0 convert(para ...
- MT【238】内心轨迹
已知$F_1,F_2$为椭圆$C:\dfrac{x^2}{4}+\dfrac{y^2}{3}=1$的左右焦点,点$P$在椭圆$C$上移动时,$\Delta{F_1PF_2}$的内心$I$的轨迹方程为_ ...
- 自学Linux Shell9.1-安装软件程序
点击返回 自学Linux命令行与Shell脚本之路 9.1-linux安装软件程序 PMS利用一个数据库来记录各种相关内容: Linux系统安装了什么软件包 每个包安装什么文件 每个已安装软件包的版本 ...
- 【bzoj3532】 Sdoi2014—Lis
http://www.lydsy.com/JudgeOnline/problem.php?id=3532 (题目链接) 题意 给出$n$个数的数列,三个值$a[i],b[i],c[i]$.将其中一些数 ...
- CDQZ.OPENJUDGE DataStructure22
我觉得这是很重要的一些题目.它们都在这里:硕巨结构 Challenge 0:给定数列,单点修改,单点查询修改.煞有介事,不过一数组耳. Challenge 1:给定数列,单点修改,单点查询第k次操作后 ...
- ST表与树状数组
ST表 st表可以解决区间最值的问题.可以做到O(nlogn)预处理 ,O(1)查询,但是不支持修改. st表的大概思路就是用st[i][j]来表示从i开始的2的j次方个树中的最值,查询时就从左端点 ...
- JSP总结(二)—Cookie(汇总)
注:后缀为汇总的基本上是整理一些网上的. 1. 什么是Cookie Cookie是Web服务器保存在用户硬盘上的一段文本.Cookie允许一个Web站点在用户电脑上保存信息并且随后再取回它 ...
- poj3070_斐波那契数列(Fibonacci)
用矩阵求斐波那契数列,快速幂log(n),只用求最后4位(加和乘的运算中前面的位数无用) #include <stdio.h> #include <stdlib.h> int ...