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:包含一个具有一定格式的字符串的对 ...
随机推荐
- USDT(omniCore)测试环境搭建
一.测试环境搭建. 注:由于window版本的omni出现同步不了的问题,推荐使用linux系统进行usdt测试链的搭建. 1.下载omnicore: wget https://bintray.com ...
- c++ 为自定义类添加stl遍历器风格的遍历方式
为仿照stl的遍历风格,实现对自定义类型的遍历. 1. 需要遍历的基础结构: struct ConnectionPtr { int id_; int port_; string addr_; //st ...
- Jquery_如何扩展方法
jQuery 别名 $ 一. 类级别扩展方法(比如$.ajax(...)) 1> 单个全局方法 $.testExtend = function (){ console.log("单个 ...
- POI 生成excel(大数据量) SXSSF
使用POI 的SXSSF (Streaming Usermodel API)生成较大的excel,同时开启压缩 import junit.framework.Assert; import org.ap ...
- Caused by: org.xml.sax.SAXParseException; lineNumber: 1
百分百是你的MYBATIS 的xml 里面的 xml节点没写对,或者忘记关闭,或者格式不对
- MT【218】交点个数
若函数$f(x)=x^3+ax^2+bx+c$有极值点$x_1,x_2$,且$f(x_1)=x_1$,则关于$x$的方程$3(f(x))^2+2af(x)+b=0$的不同实数根个数为_____ 注意到 ...
- 【ARC065E】??
Description 链接 Solution 问题其实就是从一个点出发,每次可以走与其曼哈顿距离恰好为一个常数\(d\)的点 显然不可能一一走完所有的边,这样复杂度下界至少是\(O(ans)\) 我 ...
- luogu3628 特别行动队 (斜率优化dp)
推出来式子以后斜率优化水过去就完事了 #include<cstdio> #include<cstring> #include<algorithm> #include ...
- A1056. Mice and Rice
Mice and Rice is the name of a programming contest in which each programmer must write a piece of co ...
- 跟我一起学习vue2(学习工程目录)[三]
查看生成的my-project的工程目录 首先看 build是最终发布的代码存放位置. 我查看了一下目录,里面都是Js文件 config目录里面主要是配置目录,包括端口号.如果开的项目多,可以进入in ...