android contentprovider
来源:互联网 发布:mac模拟器玩游戏 编辑:程序博客网 时间:2024/05/05 20:19
花了一天时间,把contentprovider搞明白了.大家可以去下载代码(免积分的,就是要共享,不要任何好处):http://download.csdn.net/detail/crazy_zhangqiang/4477123
创建过程:
1,(第一步)创建自己的contentprovider:我的代码如下:
public class MyContentProvider extends ContentProvider {
//这里是静态方法的调用,因此该方法被执行
private static final UriMatcher sURIMatcher = buildUriMatcher();
/**
* Builds up a UriMatcher for search suggestion and shortcut refresh queries.
*/
private static UriMatcher buildUriMatcher() {
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
// to get definitions...
matcher.addURI("com.example.content1.mycontentprovider", "grils", 1);
matcher.addURI("com.example.content1.mycontentprovider","grils/#",2);
/* The following are unused in this implementation, but if we include
* {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column in our suggestions table, we
* could expect to receive refresh queries when a shortcutted suggestion is displayed in
* Quick Search Box, in which case, the following Uris would be provided and we
* would return a cursor with a single item representing the refreshed suggestion data.
*/
return matcher;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
Log.i("Provider", "==========>mycontent");
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
switch(sURIMatcher.match(uri))
{
case 1:
Log.i("Grils","fullcontent");
break;
case 2:
Log.i("Grils","content in id");
String rowId = uri.getLastPathSegment();
Log.i("rowId=====>",rowId);
break;
}
return null;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
}
//上面的红色内容是关键,仔细研究一下,在第二个红色内容中,有log输出,证明创建成功
2.(第二步)修改androidmanifest.xml:如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.content1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name=".MyContentProvider"
android:authorities="com.example.content1.mycontentprovider"
></provider>
</application>
</manifest>
注:关注红色字体,在这里告知系统我有一个内容提供者.系统自动生成。
contentprovider己经准备好了.下面是测试;
3.(第三步)activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:onClick="btnclick"
/>
</RelativeLayout>
4.(第四步)mainactivity.java:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void btnclick(View v)
{
Uri uri = Uri.parse("content://com.example.content1.mycontentprovider/girls");
this.managedQuery(uri, null, null, null, null);
Uri uri1 = Uri.parse("content://com.example.content1.mycontentprovider/girls/22");
this.managedQuery(uri1, null, null, null, null);
}
}
运行,单击按钮,log 输出,完成
- android contentprovider
- android contentprovider
- Android ContentProvider
- android ContentProvider
- Android ContentProvider
- android ContentProvider
- android ContentProvider
- Android ContentProvider
- Android ContentProvider
- android ContentProvider
- Android ContentProvider
- android contentprovider
- android contentProvider
- Android ContentProvider
- Android ContentProvider
- Android ContentProvider
- Android ContentProvider
- android contentprovider
- 查看dd命令的执行进度
- 有关哥的传说(程序员版)
- 转:金山网络傅盛:移动互联网时代的矿工五法则
- MYSQL cmake 安装指南
- 虚拟机中的锁优化简介
- android contentprovider
- 再探地精排序GnomeSort
- PHP特级视频教程 笔记心得 第十二集 MySQL bin-log日志和主从复制
- Object-c 字符串处理及建立(包含一些数组的使用等)
- 用Map.Entry 和Map.entrySet() 遍历哈希表
- 基于Xcode4开发第一个iPhone程序:“Hello World”
- Log4cpp学习记录
- grails获取domain类所有属性
- log4cpp快速使用指南