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.pswp.cn/news/531707.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/531707.shtml
英文地址,請注明出處:http://en.pswp.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…