contentprovider java_创建Contentprovider,

创建Contentprovider:

1. 创建一个provider----ExampleContentProvider

a. 设计authority b. 设计path c.处理content URI IDs d.Content URI patterns

)

定义MIME Types(One of the required methods that you must implement for any provider.A method that you're expected to implement if your provider offers files.)

package com.hualu.contentprovider;

import java.util.HashMap;

import java.util.Map;

import android.content.ContentProvider;

import android.content.ContentUris;

import android.content.ContentValues;

import android.content.Context;

import android.content.UriMatcher;

import android.database.Cursor;

import android.database.SQLException;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.database.sqlite.SQLiteQueryBuilder;

import android.net.Uri;

import android.text.TextUtils;

public class ExampleContentProvider extends ContentProvider {

/*

* Defines a handle to the database helper object. The MainDatabaseHelper class is defined

* in a following snippet.

*/

private MainDatabaseHelper mOpenHelper;

// Defines the database name

private static final String DBNAME = "mydb";

private static final int MAINS = 1 ;

private static final int MAIN_ID = 2 ;

/**

* A UriMatcher instance

*/

private static final UriMatcher sUriMatcher;

private static Map columnMap = new HashMap() ;

static{

// Create a new instance

sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

sUriMatcher.addURI(Main.AUTHORITY, "mains", MAINS) ;

sUriMatcher.addURI(Main.AUTHORITY, "main", MAIN_ID) ;

sUriMatcher.addURI(Main.AUTHORITY, "main/#", MAIN_ID) ;

columnMap.put("id","_ID") ;

columnMap.put("word","WORD") ;

}

public boolean onCreate() {

/*

* Creates a new helper object. This method always returns quickly.

* Notice that the database itself isn't created or opened

* until SQLiteOpenHelper.getWritableDatabase is called

*/

mOpenHelper = new MainDatabaseHelper(

getContext() // the application context

);

return true;

}

@Override

public int delete(Uri arg0, String arg1, String[] arg2) {

return 0;

}

@Override

public String getType(Uri uri) {

switch (sUriMatcher.match(uri)) {

case MAINS:{

return Main.CONTENT_TYPE;

}

case MAIN_ID:{

return Main.CONTENT_ITEM_TYPE;

}

}

return null;

}

@Override

public Uri insert(Uri uri, ContentValues values) {

if(sUriMatcher.match(uri) == MAIN_ID){

throw new IllegalArgumentException("Unknown URI " + uri);

}

ContentValues value ;

if(null != values){

value = new ContentValues(values) ;

}else{

value = new ContentValues() ;

}

SQLiteDatabase db = mOpenHelper.getWritableDatabase() ;

long rowId = db.insert(

"main",

null,

values) ;

// If the insert succeeded, the row ID exists.

if (rowId > 0) {

// Creates a URI with the note ID pattern and the new row ID appended to it.

Uri noteUri = ContentUris.withAppendedId(Uri.parse(Main.CONTENT_URI + "/main/"), rowId);

// Notifies observers registered against this provider that the data changed.

getContext().getContentResolver().notifyChange(noteUri, null);

return noteUri;

}

// If the insert didn't succeed, then the rowID is <= 0. Throws an exception.

throw new SQLException("Failed to insert row into " + uri);

}

@Override

public Cursor query(Uri uri, String[] projection, String selection,

String[] selectionArgs, String sortOrder) {

SQLiteQueryBuilder sqb = new SQLiteQueryBuilder() ;

sqb.setTables("main") ;

switch (sUriMatcher.match(uri)) {

case MAINS :

sqb.setProjectionMap(columnMap) ;

break ;

case MAIN_ID :

sqb.setProjectionMap(columnMap) ;

sqb.appendWhere("_ID = " +

uri.getPathSegments().get(1)) ;

break ;

}

String orderBy;

// If no sort order is specified, uses the default

if (TextUtils.isEmpty(sortOrder)) {

orderBy = "_ID";

} else {

// otherwise, uses the incoming sort order

orderBy = sortOrder;

}

SQLiteDatabase db = mOpenHelper.getReadableDatabase();

Cursor c = sqb.query(

db,

projection,

selection,

selectionArgs,

null,

null,

orderBy) ;

c.setNotificationUri(getContext().getContentResolver(), uri);

return c;

}

@Override

public int update(Uri uri, ContentValues values, String selection,

String[] selectionArgs) {

return 0;

}

// A string that defines the SQL statement for creating a table

private static final String SQL_CREATE_MAIN = "CREATE TABLE " +

"main " + // Table's name

"(" + // The columns in the table

" _ID INTEGER PRIMARY KEY, " +

" WORD TEXT" +

" FREQUENCY INTEGER " +

" LOCALE TEXT )";

/**

* Helper class that actually creates and manages the provider's underlying data repository.

*/

protected static final class MainDatabaseHelper extends SQLiteOpenHelper {

/*

* Instantiates an open helper for the provider's SQLite data repository

* Do not do database creation and upgrade here.

*/

MainDatabaseHelper(Context context) {

super(context, DBNAME, null, 1);

}

/*

* Creates the data repository. This is called when the provider attempts to open the

* repository and SQLite reports that it doesn't exist.

*/

public void onCreate(SQLiteDatabase db) {

// Creates the main table

db.execSQL(SQL_CREATE_MAIN);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

}

2.定义权限

在manifest 中定义,permission

在节点里面,定义permission

3.provider添加权限

在 节点里面添加

android:writePermission和android:readPermission

android:writePermission="com.hualu.provider.WRITE"

android:readPermission="com.hualu.provider.READ">

在另一个应用访问这个contentprovider

1.新建一个应用

2.在当前应用的manifest里面添加对之前定义的provider的权限的使用

3.在Activity里面通过ContentResolver调用provider

ContentValues values = new ContentValues() ;

values.put("WORD", "abcd") ;

Uri uri = this.getContentResolver().insert(

Uri.parse("content://com.hualu.contentprovider/mains"),

values) ;

String id = uri.getPathSegments().get(1) ;

Cursor cAll = this.getContentResolver().query(

Uri.parse("content://com.hualu.contentprovider/mains"),

null,

null,

null,

null);

Cursor c = this.getContentResolver().query(

Uri.parse("content://com.hualu.contentprovider/main/1"),

null,

null,

null,

null);

Toast.makeText(this, "insert success id = " + id + " ," +

" \r\n All = " + cAll.getCount() + " , " +

"\r\n one = " + c.getCount(),

Toast.LENGTH_SHORT).show() ;

代码下载地址:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/531707.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hdu Caocao's Bridges(无向图边双连通分量,找出权值最小的桥)

1 /*2 题意&#xff1a;给出一个无向图&#xff0c;去掉一条权值最小边&#xff0c;使这个无向图不再连同&#xff01;3 4 tm太坑了...5 1,如果这个无向图开始就是一个非连通图&#xff0c;直接输出06 2&#xff0c;重边&#xff08;两个节点存在多条边&am…

poj1273Drainage Ditches

1 #include<iostream>2 /*3 题意&#xff1a;就是寻找从源点到汇点的最大流&#xff01;4 要注意的是每两个点的流量可能有多个&#xff0c;也就是说有重边&#xff0c;所以要把两个点的所有的流量都加起来5 就是这两个点之间的流量了&#xff0…

Java11.0.2怎么生成JRE_java环境变量配置,jdk13.0.1中没有jre解决办法

标签&#xff1a;完成后 回车 手动 完成 cmd 没有 alt span 环境变量配置java.Oracle中下载了最新的jdk13.0.1&#xff0c;安装之后发现没自动生成jre&#xff0c;导致环境变量配置一直不成功如果没有自动生成jre&#xff0c;需要手动生成jre手动生成办法&…

hdu4751Divide Groups(dfs枚举完全图集合或者bfs染色)

1 /*************************************************************************2 > File Name: j.cpp3 > Author: HJZ4 > Mail: 2570230521qq.com 5 > Created Time: 2014年08月28日 星期四 12时26分13秒6 ***********************************…

java二期_享学二期java架构师

前言-薇:itstudy01在我们工作和学习的过程中&#xff0c;Java线程我们或多或少的都会用到&#xff0c;但是在使用的过程上并不是很顺利&#xff0c;会遇到各种各样的坑&#xff0c;这里我通过讲解Thread类中的核心方法&#xff0c;以求重点掌握以下关键技术点&#xff1a;线程的…

poj3342Party at Hali-Bula(树形dp)

1 /*2 树形dp&#xff01;3 判重思路&#xff1a;4 当dp[v][0]dp[v][1]时&#xff0c;很自然&#xff0c;flag[u][0]必然是有两种方案的。flag[u][1]则不然&#xff0c;5 因为它只和dp[v][0]有关系。而若flag[v][0]不唯一时&#xff0c;则必然flag[u][1]也不唯一6 …

mysql django构架图_(一)Django项目架构介绍

项目的架构为&#xff1a;1、虚拟环境virtualenv安装Django2.1.3安装pymysql安装mysqlclient安装其他等2、项目结构为&#xff1a;应用APP&#xff1a;blog -- 管理博客account -- 管理用户注册/登录/等后台数据库&#xff1a;mysql路由分层及命名空间&#xff1a;根据应用进行…

poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)

LCA思想&#xff1a;http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上&#xff0c;用到的是Tarjan的思想&#xff0c;从根结点开始形成一棵深搜树&#xff0c;非常好的处理技巧就是在回溯到结点u的时候&#xff0c;u的子树已经遍历&#xff0c;这…

LCA算法的理解

LCA思想&#xff1a;在求解最近公共祖先为问题上&#xff0c;用到的是Tarjan的思想&#xff0c;从根结点开始形成一棵深搜树&#xff0c;非常好的处理技巧就是在回溯到结点u的时候&#xff0c;u的子树已经遍历&#xff0c;这时候才把u结点放入合并集合中&#xff0c; 这样u结点…

java连加密的mysql_Java 实现加密数据库连接

一、前言在很多项目中&#xff0c;数据库相关的配置文件内容都是以明文的形式展示的&#xff0c;这存在一定的安全隐患。在开发和维护项目时&#xff0c;不仅要关注项目的性能&#xff0c;同时也要注重其安全性。二、实现思路我们都知道项目启动时&#xff0c;Spring 容器会加载…

codeforces Gargari and Bishops(很好的暴力)

1 /*2 题意&#xff1a;给你一个n*n的格子&#xff0c;每一个格子都有一个数值&#xff01;将两只bishops放在某一个格子上&#xff0c;3 每一个bishop可以攻击对角线上的格子&#xff08;主对角线和者斜对角线&#xff09;&#xff0c;然后会获得格子上的4 数值&a…

java词汇速查手册_java 词汇表速查手册

Abstract class 抽象类:抽象类是不允许实例化的类&#xff0c;因此一般它需要被进行扩展继承。Abstract method 抽象方法:抽象方法即不包含任何功能代码的方法。Access modifier 访问控制修饰符:访问控制修饰符用来修饰Java中类、以及类的方法和变量的访问控制属性。Anonymous …

codeforces Gargari and Permutations(DAG+BFS)

1 /*2 题意&#xff1a;求出多个全排列的lcs&#xff01;3 思路&#xff1a;因为是全排列&#xff0c;所以每一行的每一个数字都不会重复&#xff0c;所以如果有每一个全排列的数字 i 都在数字 j的前面&#xff0c;那么i&#xff0c; j建立一条有向边&#xff01;4 …

hdu4292Food(最大流Dinic算法)

/*    题意&#xff1a;每一个人都有喜欢的吃的和喝的&#xff0c;每一个人只选择一个数量的吃的和一个数量的喝的&#xff0c;问能满足最多的人数&#xff01;&#xff1f;    思路&#xff1a;建图很是重要&#xff01;f-food, p-people, d-drink    建图&#x…

python3.5 连接mysql_python3.5 連接mysql本地數據庫

前期准備工作&#xff1a;安裝python的模塊&#xff0c;網上大部分讓安裝mysqldb模塊&#xff0c;但是會報錯&#xff0c;原因是python3.5不被其支持&#xff1a;請看該鏈接 我們也可以這樣解決&#xff1a;直接執行&#xff1a;sudo pip3 install pymysql;在python3中輸入impo…

java异常顺序_网易新闻

public class SmallT {public static void main(String args[]) {SmallT t new SmallT();int b t.get();System.out.println(b);}public int get() {try {return 1;} finally {return 2;}}}返回的结果是2。我可以通过下面一个例子程序来帮助我解释这个答案&#xff0c;从下面…

java中自动装箱的问题

package wrapper;public class WrapperDemo {public static void main(String[] args) {Integer anew Integer(5);Integer bnew Integer(5);System.out.println(ab);System.out.println(a.equals(b));/*falsetrue*/Integer c127;//属于自动装箱Integer d127;//jdk1.5以后&#…

下载国外网站资料需java_Java开发必知道的国外10大网站

1、https://www.google.com/不解释2、https://stackoverflow.com里面包含各种开发遇到的问题及答案&#xff0c;质量比较高。3、https://github.com/免费的开源代码托管网站&#xff0c;包括了许多开源的项目及示例项目等。4、https://dzone.com/提供技术新闻、编程教程、及各种…

poj 1950 Dessert(dfs枚举,模拟运算过程)

/*   这个代码运行的时间长主要是因为每次枚举之后都要重新计算一下和的值&#xff01;    如果要快的话&#xff0c;应该在dfs&#xff0c;也就是枚举的过程中计算出前边的数值&#xff08;这种方法见第二个代码&#xff09;&#xff0c;直到最后&#xff0c;这样不必每…

poj1949Chores(建图或者dp)

1 /*2 题意&#xff1a;n个任务&#xff0c;有某些任务要在一些任务之前完成才能开始做&#xff01;3 第k个任务的约束只能是1...k-1个任务&#xff01;问最终需要最少的时间完成全部的 4 任务&#xff0…