建立自己的ContentProvider
public class LauncherLiveFolderProvider extends ContentProvider
//自己建立一个类,继承自ContentProvider, 默认就有这些需要实现的方法
//如果只是用来读取数据的话,实现query和getType什么的就基本齐活儿了。
//
转一个最近写的把sms和bookmark做到livefolder的代码,参考ahome和contacts源码弄的
provider定义的比较随意,似乎丑陋了点。。。
源码没读多少,sms那块儿完全就是自己看了数据库表项之后随便凑合的。
1。LauncherLiveFolderAdapterURI.java。 全是变量。
package g.LauncherLiveFolderAdapter;import android.net.Uri;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"); // 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= "vnd.android.cursor.item/bookmark"; public static final String SMS_CONTENT_TYPE = "vnd.android.cursor.dir/mms-sms"; public static final String SMS_CONTENT_TYPE_ITEM="vnd.android.cursor.item/mms-sms"; }package g.LauncherLiveFolderAdapter;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.provider.Browser;import android.provider.LiveFolders;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)); } else { setResult(RESULT_CANCELED); } finish(); } } 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)); } else { setResult(RESULT_CANCELED); } finish(); } } 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)); } else { setResult(RESULT_CANCELED); } finish(); } } 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)); } else { setResult(RESULT_CANCELED); } finish(); } } 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)); } else { setResult(RESULT_CANCELED); } finish(); } } 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)); } else { setResult(RESULT_CANCELED); } finish(); } } 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)); } else { setResult(RESULT_CANCELED); } finish(); } } 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); return intent; }}package g.LauncherLiveFolderAdapter;import android.content.ContentProvider;import android.content.ContentValues;import android.content.UriMatcher;import android.database.Cursor;import android.database.MatrixCursor;import android.net.Uri;import android.provider.Browser;import android.provider.LiveFolders;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 sMatcher; static{ 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); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { throw new NoSuchMethodError(); } @Override public Uri insert(Uri uri, ContentValues values) { throw new NoSuchMethodError(); } @Override public boolean onCreate() { return true; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { throw new NoSuchMethodError(); } @Override 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; default: throw new IllegalArgumentException("Unknown URI g:"+uri); } } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Uri query_uri; String[] prejection; 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 = null; break; 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 break; 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 break; // 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 = null; break; 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 = null; break; 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 = null; break; 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 = null; break; default: throw new IllegalArgumentException("Unknown URI g:"+uri); } Cursor c = getContext().getContentResolver().query(query_uri, prejection, selection,null, sortOrder); // DESC 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().length; while (c.moveToNext()) { if(intent_uri_type==1) intent_uri =c.getString(Browser.HISTORY_PROJECTION_URL_INDEX); else intent_uri = SMS_CONVERSATION_URI+c.getString(1);// conversation_id @ sms , don't know any statics 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 // name to find that right activity // for bookmarks, uri is like http:// ..... }; mc.addRow(row); } return mc; }}<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/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-permissionandroid:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"></uses-permission> <uses-permission android:name="android.permission.READ_SMS"/></manifest>