日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

contentprovider java_创建Contentprovider,

發(fā)布時(shí)間:2025/3/8 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 contentprovider java_创建Contentprovider, 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

創(chuàng)建Contentprovider:

1. 創(chuàng)建一個(gè)provider----ExampleContentProvider

a. 設(shè)計(jì)authority b. 設(shè)計(jì)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.定義權(quán)限

在manifest 中定義,permission

在節(jié)點(diǎn)里面,定義permission

3.provider添加權(quán)限

在 節(jié)點(diǎn)里面添加

android:writePermission和android:readPermission

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

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

在另一個(gè)應(yīng)用訪問這個(gè)contentprovider

1.新建一個(gè)應(yīng)用

2.在當(dāng)前應(yīng)用的manifest里面添加對之前定義的provider的權(quán)限的使用

3.在Activity里面通過ContentResolver調(diào)用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() ;

代碼下載地址:

總結(jié)

以上是生活随笔為你收集整理的contentprovider java_创建Contentprovider,的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。