contentprovider使用建立问题

本帖子已过去太久远了,不再提供回复功能。> 建立自各儿的ContentProvider
建立自各儿的ContentProvider
liuhanwu052 & &
发布时间: & &
浏览:34 & &
回复:1 & &
悬赏:0.0希赛币
建立自己的ContentProvider
public class LauncherLiveFolderProvider extends ContentProvider//自己建立一个类,继承自ContentProvider, 默认就有这些需要实现的方法//如果只是用来读取数据的话,实现query和getType什么的就基本齐活儿了。//转一个最近写的把sms和bookmark做到livefolder的代码,参考ahome和contacts源码弄的provider定义的比较随意,似乎丑陋了点。。。源码没读多少,sms那块儿完全就是自己看了数据库表项之后随便凑合的。1。LauncherLiveFolderAdapterURI.java。 全是变量。  package g.LauncherLiveFolderA
import android.net.U
public class LauncherLiveFolderAdapterURI {
public static final String AUTHORITY_SMS="livefolder.sms";
public static final String AUTHORITY_BOOKMARK="livefolder.browser";
public static final Uri BOOKMARK_ALL_CONTNET_URI = Uri.parse("content://"+AUTHORITY_BOOKMARK+"/bookmarks/all");
public static final Uri BOOKMARK_RECENTVIST_CONTNET_URI = Uri.parse("content://"+AUTHORITY_BOOKMARK+"/bookmarks/recently_visited");
public static final Uri BOOKMARK_MOSTVIST_CONTNET_URI = Uri.parse("content://"+AUTHORITY_BOOKMARK+"/bookmarks/most_visited");
public static final Uri SMS_ALL_CONTENT_URI = Uri.parse("content://"+AUTHORITY_SMS+"/all");
= inbox + sent
public static final Uri SMS_INBOX_CONTENT_URI = Uri.parse("content://"+AUTHORITY_SMS+"/inbox");
public static final Uri SMS_SENT_CONTENT_URI = Uri.parse("content://"+AUTHORITY_SMS+"/sent");
public static final Uri SMS_UNREAD_CONTENT_URI = Uri.parse("content://"+AUTHORITY_SMS+"/unread");
// unread & inbox
public static final Uri CONTNET_SMS_URI =
Uri.parse("content://"+AUTHORITY_SMS);
public static final int SMS_ALL = 101;
public static final int SMS_INBOX = 102;
public static final int SMS_UNREAD = 103;
public static final int SMS_SENT = 104;
public static final int BOOKMARK_ALL = 111;
public static final int BOOKMARK_RECENT = 112;
public static final int BOOKMARK_MOST = 113;
public static final String BOOKMARK_CONTENT_TYPE = "vnd.android.cursor.dir/bookmark";
public static final String BOOKMARK_CONTENT_TYPE_ITEM=
"vnem/bookmark";
public static final String SMS_CONTENT_TYPE = "vnd.android.cursor.dir/mms-sms";
public static final String SMS_CONTENT_TYPE_ITEM="vnem/mms-sms";
2。LauncherLiveFolderAdapter.java 是用来在livefolder选项menu里面填满应用的家伙  package g.LauncherLiveFolderA
import android.app.A
import android.content.C
import android.content.I
import android.net.U
import android.os.B
import android.provider.B
import android.provider.LiveF
public class LauncherLiveFolderAdapter {
public static class AllBookmark extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
final String action = intent.getAction();
String s = "All bookmarks"; // name returned to launcher. show on the main
if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {
setResult(RESULT_OK, createLiveFolder(this,
LauncherLiveFolderAdapterURI.BOOKMARK_ALL_CONTNET_URI, s, R.drawable.icon));
setResult(RESULT_CANCELED);
public static class MostBookmark extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
final String action = intent.getAction();
String s = "Most visited bookmarks";
if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {
setResult(RESULT_OK, createLiveFolder(this,
LauncherLiveFolderAdapterURI.BOOKMARK_MOSTVIST_CONTNET_URI, s,
R.drawable.icon));
setResult(RESULT_CANCELED);
public static class RecentBookmark extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
final String action = intent.getAction();
String s = "Recently visited bookmarks";
if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {
setResult(RESULT_OK, createLiveFolder(this,
LauncherLiveFolderAdapterURI.BOOKMARK_RECENTVIST_CONTNET_URI, s,
R.drawable.icon));
setResult(RESULT_CANCELED);
public static class AllSMS extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
final String action = intent.getAction();
String s = "ALL SMS";
if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {
setResult(RESULT_OK, createLiveFolder(this,
LauncherLiveFolderAdapterURI.SMS_ALL_CONTENT_URI, s,
R.drawable.icon));
setResult(RESULT_CANCELED);
public static class InboxSMS extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
final String action = intent.getAction();
String s = "Inbox SMS";
if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {
setResult(RESULT_OK, createLiveFolder(this,
LauncherLiveFolderAdapterURI.SMS_INBOX_CONTENT_URI, s,
R.drawable.icon));
setResult(RESULT_CANCELED);
public static class SentSMS extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
final String action = intent.getAction();
String s = "Sent SMS";
if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {
setResult(RESULT_OK, createLiveFolder(this,
LauncherLiveFolderAdapterURI.SMS_SENT_CONTENT_URI, s,
R.drawable.icon));
setResult(RESULT_CANCELED);
public static class UnreadSMS extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
final String action = intent.getAction();
String s = "Unread SMS";
if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {
setResult(RESULT_OK, createLiveFolder(this,
LauncherLiveFolderAdapterURI.SMS_UNREAD_CONTENT_URI, s,
R.drawable.icon));
setResult(RESULT_CANCELED);
private static Intent createLiveFolder(Context context, Uri uri, String name, int icon) {
final Intent intent = new Intent();
intent.setData(uri);
intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME, name);
intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON, Intent.ShortcutIconResource
.fromContext(context, icon));
intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, LiveFolders.DISPLAY_MODE_LIST);
3 。 LauncherLiveFolderProvider.java , 因为只要读就行,简单不少  package g.LauncherLiveFolderA
import android.content.ContentP
import android.content.ContentV
import android.content.UriM
import android.database.C
import android.database.MatrixC
import android.net.U
import android.provider.B
import android.provider.LiveF
public class LauncherLiveFolderProvider extends ContentProvider {
public static final String AUTHORITY_SMS="livefolder.sms";
public static final String AUTHORITY_BOOKMARK="livefolder.browser";
// used to query
provided by system
public static final Uri SMS_URI_ALL = Uri.parse("content://sms/");
public static final Uri SMS_URI_INBOX = Uri.parse("content://sms/inbox");
public static final Uri SMS_URI_SENT= Uri.parse("content://sms/sent");
public static final String SMS_CONVERSATION_URI= "content://mms-sms/conversations/";
private static final UriMatcher sM
sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sMatcher.addURI(LauncherLiveFolderAdapterURI.AUTHORITY_BOOKMARK, "bookmarks/all", LauncherLiveFolderAdapterURI.BOOKMARK_ALL);
sMatcher.addURI(LauncherLiveFolderAdapterURI.AUTHORITY_BOOKMARK, "bookmarks/most_visited", LauncherLiveFolderAdapterURI.BOOKMARK_MOST);
sMatcher.addURI(LauncherLiveFolderAdapterURI.AUTHORITY_BOOKMARK, "bookmarks/recently_visited", LauncherLiveFolderAdapterURI.BOOKMARK_RECENT);
sMatcher.addURI(LauncherLiveFolderAdapterURI.AUTHORITY_SMS, "all", LauncherLiveFolderAdapterURI.SMS_ALL);
sMatcher.addURI(LauncherLiveFolderAdapterURI.AUTHORITY_SMS, "sent", LauncherLiveFolderAdapterURI.SMS_SENT);
sMatcher.addURI(LauncherLiveFolderAdapterURI.AUTHORITY_SMS, "unread", LauncherLiveFolderAdapterURI.SMS_UNREAD);
sMatcher.addURI(LauncherLiveFolderAdapterURI.AUTHORITY_SMS, "inbox", LauncherLiveFolderAdapterURI.SMS_INBOX);
public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new NoSuchMethodError();
public Uri insert(Uri uri, ContentValues values) {
throw new NoSuchMethodError();
public boolean onCreate() {
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
throw new NoSuchMethodError();
public String getType(Uri uri) {
switch(sMatcher.match(uri)){
case LauncherLiveFolderAdapterURI.BOOKMARK_ALL:
return LauncherLiveFolderAdapterURI.BOOKMARK_CONTENT_TYPE;
case LauncherLiveFolderAdapterURI.BOOKMARK_MOST:
return LauncherLiveFolderAdapterURI.BOOKMARK_CONTENT_TYPE;
case LauncherLiveFolderAdapterURI.BOOKMARK_RECENT:
return LauncherLiveFolderAdapterURI.BOOKMARK_CONTENT_TYPE;
case LauncherLiveFolderAdapterURI.SMS_ALL:
return LauncherLiveFolderAdapterURI.SMS_CONTENT_TYPE;
case LauncherLiveFolderAdapterURI.SMS_INBOX:
return LauncherLiveFolderAdapterURI.SMS_CONTENT_TYPE;
case LauncherLiveFolderAdapterURI.SMS_SENT:
return LauncherLiveFolderAdapterURI.SMS_CONTENT_TYPE;
case LauncherLiveFolderAdapterURI.SMS_UNREAD:
return LauncherLiveFolderAdapterURI.SMS_CONTENT_TYPE;
throw new IllegalArgumentException("Unknown URI g:"+uri);
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
Uri query_
int id_index=0;
int name_index=0;
int description_index=0;
int intent_uri_type = -1;
String intent_uri="";
int matched = sMatcher.match(uri);
switch(matched){
case LauncherLiveFolderAdapterURI.BOOKMARK_ALL:
query_uri = Browser.BOOKMARKS_URI;
prejection = Browser.HISTORY_PROJECTION;
id_index = Browser.HISTORY_PROJECTION_ID_INDEX;
name_index = Browser.HISTORY_PROJECTION_TITLE_INDEX;
description_index = Browser.HISTORY_PROJECTION_URL_INDEX;
intent_uri_type = Browser.HISTORY_PROJECTION_URL_INDEX;
selection = "bookmark=1";
//boookmarked
sortOrder =
case LauncherLiveFolderAdapterURI.BOOKMARK_MOST:
query_uri = Browser.BOOKMARKS_URI;
prejection = Browser.HISTORY_PROJECTION;
id_index = Browser.HISTORY_PROJECTION_ID_INDEX;
name_index = Browser.HISTORY_PROJECTION_TITLE_INDEX;
description_index = Browser.HISTORY_PROJECTION_URL_INDEX;
intent_uri_type = Browser.HISTORY_PROJECTION_URL_INDEX;
selection = "visits & 0";
sortOrder = "visits desc";
//most visited
case LauncherLiveFolderAdapterURI.BOOKMARK_RECENT:
query_uri = Browser.BOOKMARKS_URI;
prejection = Browser.HISTORY_PROJECTION;
id_index = Browser.HISTORY_PROJECTION_ID_INDEX;
name_index = Browser.HISTORY_PROJECTION_TITLE_INDEX;
description_index = Browser.HISTORY_PROJECTION_URL_INDEX;
intent_uri_type = Browser.HISTORY_PROJECTION_URL_INDEX;
selection = "date & 0";
sortOrder = "date desc";
// most recently visited
// for sms
case LauncherLiveFolderAdapterURI.SMS_ALL:
query_uri = SMS_URI_ALL;
id_index = 0;
name_index = 11; //body
description_index = 4; // person
intent_uri_type = 2; //type .
prejection = new String[]{"*"};
sortOrder =
case LauncherLiveFolderAdapterURI.SMS_INBOX:
query_uri = SMS_URI_INBOX;
id_index = 0;
name_index = 11; //body
description_index = 4; // person
intent_uri_type = 2; //type .
prejection = new String[]{"*"};
sortOrder =
case LauncherLiveFolderAdapterURI.SMS_SENT:
query_uri = SMS_URI_SENT;
id_index = 0;
name_index = 11; //body
description_index = 4; // person
intent_uri_type = 2; //type .
prejection = new String[]{"*"};
sortOrder =
case LauncherLiveFolderAdapterURI.SMS_UNREAD:
query_uri = SMS_URI_INBOX;
// inbox, unread
name_index = 11; //body
description_index = 4; // person
intent_uri_type = 2; //type .
prejection = new String[]{"*"};
id_index = 0;
sortOrder =
throw new IllegalArgumentException("Unknown URI g:"+uri);
Cursor c = getContext().getContentResolver().query(query_uri,
prejection, selection,null, sortOrder);
String[] liveFolderColumnNames = {
LiveFolders._ID,
LiveFolders.NAME,
LiveFolders.DESCRIPTION,
LiveFolders.ICON_PACKAGE, //icon added
LiveFolders.ICON_RESOURCE, //
LiveFolders.INTENT
MatrixCursor mc = new MatrixCursor(liveFolderColumnNames, c.getCount());
int columnCount = c.getColumnNames().
while (c.moveToNext()) {
if(intent_uri_type==1)
intent_uri =c.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
intent_uri = SMS_CONVERSATION_URI+c.getString(1);// conversation_id @ sms , don't know any
Object[] row = {
c.getString(id_index), // for id
c.getString(name_index), // for name
c.getString(description_index),//for description
getContext().getPackageName(), //icon package
R.drawable.icon, //icon resource
Uri.parse(intent_uri)
//intent type uri
that right activity //
for bookmarks,
uri is like
mc.addRow(row);
最后menifest.xml文件  & xml version="1.0" encoding="utf-8" &
&manifest xmlns:android=""
package="g.LauncherLiveFolderAdapter" android:versionCode="1"
android:versionName="1.0"&
&application android:icon="@drawable/icon" android:label="@string/app_name"&
&provider android:name=".LauncherLiveFolderProvider"
android:authorities="livefolder.browser" /&
&provider android:name=".LauncherLiveFolderProvider"
android:authorities="livefolder.sms" /&
&activity android:name=".LauncherLiveFolderAdapter$AllBookmark"
android:label="All Bookmark"
android:icon="@drawable/icon"&
&intent-filter&
&action android:name="android.intent.action.CREATE_LIVE_FOLDER" /&
&category android:name="android.intent.category.DEFAULT" /&
&/intent-filter&
&/activity&
&activity android:name=".LauncherLiveFolderAdapter$MostBookmark"
android:label="Most Visited Bookmark"
android:icon="@drawable/icon"&
&intent-filter&
&action android:name="android.intent.action.CREATE_LIVE_FOLDER" /&
&category android:name="android.intent.category.DEFAULT" /&
&/intent-filter&
&/activity&
&activity android:name=".LauncherLiveFolderAdapter$RecentBookmark"
android:label="Recently Visited Bookmark"
android:icon="@drawable/icon"&
&intent-filter&
&action android:name="android.intent.action.CREATE_LIVE_FOLDER" /&
&category android:name="android.intent.category.DEFAULT" /&
&/intent-filter&
&/activity&
&activity android:name=".LauncherLiveFolderAdapter$AllSMS"
android:label="All SMS"
android:icon="@drawable/icon"&
&intent-filter&
&action android:name="android.intent.action.CREATE_LIVE_FOLDER" /&
&category android:name="android.intent.category.DEFAULT" /&
&/intent-filter&
&/activity&
&activity android:name=".LauncherLiveFolderAdapter$SentSMS"
android:label="SMS SENT"
android:icon="@drawable/icon"&
&intent-filter&
&action android:name="android.intent.action.CREATE_LIVE_FOLDER" /&
&category android:name="android.intent.category.DEFAULT" /&
&/intent-filter&
&/activity&
&activity android:name=".LauncherLiveFolderAdapter$InboxSMS"
android:label="SMS INBOX"
android:icon="@drawable/icon"&
&intent-filter&
&action android:name="android.intent.action.CREATE_LIVE_FOLDER" /&
&category android:name="android.intent.category.DEFAULT" /&
&/intent-filter&
&/activity&
&activity android:name=".LauncherLiveFolderAdapter$UnreadSMS"
android:label="SMS UNREAD"
android:icon="@drawable/icon"&
&intent-filter&
&action android:name="android.intent.action.CREATE_LIVE_FOLDER" /&
&category android:name="android.intent.category.DEFAULT" /&
&/intent-filter&
&/activity&
&/application&
&uses-sdk android:minSdkVersion="3" /&
&uses-permission
android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"$>$/uses-permission&
&uses-permission android:name="android.permission.READ_SMS"/&
&/manifest&
  您好,我自己写了一个ContentProvider-MyProvider在mainfest.xml文件中通过android:name="android.permission.WRITE_MYPERMISSION"设置了写权限,然后通过自己写的另一个apk去访问这个provider的时候,通过在mainfest&xml文件中添加&uses-permission android:name="android.permission.WRITE_MYPERMISSION"&这样设置了访问权限,但是为什么我的程序已运行还是报错说缺少访问权限啊?您给指点一下?谢谢liuhao51 & &
09:57:29 & &
& & (0)(0)
本问题标题:
本问题地址:
温馨提示:本问题已经关闭,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&一个关于ContentProvider的问题_android开发吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:110,304贴子:
一个关于ContentProvider的问题收藏
一个类继承了ContentProvider 在它的getType内部返回值中 "vnd.android.cursor.item/person" 我想问下 person是什么意思 是自定义的类 还是什么 在线等
「达内」上海android开发培训,上市公司出品,重磅推出「因材施教,分级教学」模式;达内上海android开发培训,就业/培优/才高三种班型,只培养&混编型&,&架构级&工程师!
大概是知道了 person是应用下的一张表 如果返回值是"vnd.android.cursor.item/com.abc.desc" 那么就是访问手机文件com.abc.desc文件夹下的数据库文件
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或2010年8月 移动平台大版内专家分月排行榜第二2010年4月 移动平台大版内专家分月排行榜第二
2010年9月 移动平台大版内专家分月排行榜第三
2010年8月 移动平台大版内专家分月排行榜第二2010年4月 移动平台大版内专家分月排行榜第二
2010年9月 移动平台大版内专家分月排行榜第三
2010年8月 移动平台大版内专家分月排行榜第二2010年4月 移动平台大版内专家分月排行榜第二
2010年9月 移动平台大版内专家分月排行榜第三
2010年8月 移动平台大版内专家分月排行榜第二2010年4月 移动平台大版内专家分月排行榜第二
2010年9月 移动平台大版内专家分月排行榜第三
2010年8月 移动平台大版内专家分月排行榜第三
2010年8月 移动平台大版内专家分月排行榜第二2010年4月 移动平台大版内专家分月排行榜第二
2010年9月 移动平台大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 contentprovider 权限 的文章

 

随机推荐