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内容提供者的更多相关文章

  1. Android -- ContentProvider 内容提供者,创建和调用

    1. 概述 ContentProvider 在android中的作用是对外共享数据,也就是说你可以通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过ContentPr ...

  2. Android组件系列----ContentProvider内容提供者

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  3. android 53 ContentProvider内容提供者

    ContentProvider内容提供者:像是一个中间件一样,一个媒介一样,可以以标准的增删改差操作对手机的文件.数据库进行增删改差.通过ContentProvider查找sd卡的音频文件,可以提供标 ...

  4. Android中内容提供者ContentProvider的详解

    1.什么是ContentProvider 首先,ContentProvider(内容提供者)是android中的四大组件之一,但是在一般的开发中,可能使用的比较少. ContentProvider为不 ...

  5. Android组件系列----ContentProvider内容提供者【1】

    [正文] 一.ContentProvider简单介绍: ContentProvider内容提供者(四大组件之中的一个)主要用于在不同的应用程序之间实现数据共享的功能. ContentProvider能 ...

  6. Android基础内容提供者ContentProvider的使用详解(转)

    1.什么是ContentProvider 首先,ContentProvider(内容提供者)是android中的四大组件之一,但是在一般的开发中,可能使用的比较少. ContentProvider为不 ...

  7. contentProvider内容提供者

    contentProvider内容提供者 15. 四 / android基础 / 没有评论   步骤 权限在application中注册 Source code     <provider an ...

  8. Android 中内容提供者的使用

    在Android中内容提供者主要是用于不同程序之间的数据共享.内容提供器的用法一般有两种,一种是使用现有的内容提供器来读取和操作相应程序的数据,另一种是创建自己的内容提供器,供其他的程序访问. 使用现 ...

  9. Android开发学习—— ContentProvider内容提供者

    * 应用的数据库是不允许其他应用访问的* 内容提供者的作用就是让别的应用访问到你的数据库.把私有数据暴露给其他应用,通常,是把私有数据库的数据暴露给其他应用. Uri:包含一个具有一定格式的字符串的对 ...

随机推荐

  1. Java微信二次开发(六)

    Token定时获取 需要导入库:添加log4j(slf4j-api-1.5.10.jar,slf4j-log4j12-1.5.10.jar,log4j-1.2.15.jar,并且在src下添加log4 ...

  2. C#遍历类的属性,然后给其赋值

    public class PP { public string a { get; set; } public string b { get; set; } public string c { get; ...

  3. 百度/头条合作命中注定!中国新BAT要来了

    据外媒报道,今日头条母公司字节跳动(ByteDace)将为中国互联网传统BAT的格局,带来一些新的活力.这家增速飞快的新闻.视频App“制造者”已经估值高达750亿美元,与三巨头之一的百度平起平坐,后 ...

  4. 云时代的IT运维面临将会有哪些变化

    导读 每一次IT系统的转型,运维系统和业务保障都是最艰难的部分.在当前企业IT系统向云架构转型的时刻,运维系统再一次面临着新的挑战.所以在数据中心运维的时候,运维人员应该注意哪些问题? 在云计算时代, ...

  5. ansible系列8-SSH连接和执行性能优化

    1. 当你的SSH的版本高于5.6时 我们可以直接修改 /etc/ansible/ansible.cfg里面的参数 ssh_args = -C -o ControlMaster=auto -o Con ...

  6. linux 内核参数优化----最大线程数限制及当前线程数查询

    1.总结系统限制有: /proc/sys/kernel/pid_max #查系统支持的最大线程数,一般会很大,相当于理论值 /proc/sys/kernel/thread-max max_user_p ...

  7. Java 的类加载机制

    Java 的类加载机制 来源 https://www.cnblogs.com/xiaoxi/p/6959615.html 一.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内 ...

  8. 【BZOJ3668】【NOI2014】起床困难综合症(贪心)

    [NOI2014]起床困难综合症(贪心) 题面 Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚 ...

  9. [系统]安装fedora 19

    再也没有什么大道至简了. ==== 步骤如下: 1. 备份. 2. 刻镜像. 选fedora-kde,gnome呵呵. 3. 分区,格式化,安装. 上面3步没什么好说的,按照官网installatio ...

  10. oracle调用DLL

    具体步骤:1.创建Oracle Library  Create Library  AAA as  'C:\UserData\xuxia\TestProc\Debug\TestProc.dll' 可以通 ...