活动介绍
file-type

ContentUris详解:Android中的URI操作

PPT文件

下载需积分: 9 | 2.29MB | 更新于2024-08-18 | 39 浏览量 | 6 评论 | 3 下载量 举报 收藏
download 立即下载
本文主要介绍了ContentUris类在Android开发中的使用,以及3G应用开发的相关背景知识,包括3G的定义、不同3G制式和2.5G的过渡技术,以及智能手机软件平台,尤其是Android平台的重要性和安装Android SDK及Eclipse插件的基本步骤。 在Android开发中,ContentUris类是一个关键工具,用于处理与内容提供者(Content Provider)相关的Uri。ContentUris类有两个主要方法: 1. `withAppendedId(uri, id)`:此方法用于在给定的Uri路径后添加一个ID。例如,如果初始Uri是`content://cn.itcast.provider.personprovider/person`,通过调用`withAppendedId(uri, 10)`,可以生成新的Uri`content://cn.itcast.provider.personprovider/person/10`,这在需要标识特定记录时非常有用。 2. `parseId(uri)`:这个方法的作用是从Uri中解析出ID部分。如果Uri是`content://cn.itcast.provider.personprovider/person/10`,调用`parseId(uri)`会返回10,帮助开发者获取到Uri所代表的资源ID。 3G,即第三代数字通信技术,是融合了无线通信与国际互联网等多媒体通信的新型移动通信系统。3G标准包括WCDMA、CDMA2000和TD-SCDMA,分别被中国联通、中国电信和中国移动采用。2.5G作为从2G到3G的过渡技术,主要包括CDMA20001X和GPRS,分别应用于中国联通和中国移动的网络。 在智能手机软件平台方面,列举了一些常见的操作系统,如Symbian、Windows Mobile、RIM BlackBerry、Android、iPhone、Palm、Brew和Java/J2ME。其中,Android自2007年由Google发布以来,逐渐成为主流,尤其在3G应用开发领域展现出巨大潜力。 对于开发者而言,要开始Android开发,首先需要安装Java Development Kit (JDK) 5或6,并且设置好开发环境。Android SDK包含了开发Android应用程序所需的所有工具,而Eclipse插件则使得在Eclipse集成开发环境中编写、调试和测试Android应用变得更加方便。安装这两个组件是进行Android开发的基础步骤。

相关推荐

filetype

代码如下:package com.example.myapplication; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.util.Log; public class DatabaseProvider extends ContentProvider { // 授权标识 public static final String AUTHORITY = "com.example.myapplication.provider"; public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/person"); public static final String COLUMN_ID = "id"; public static final String COLUMN_NAME = "name"; public static final String COLUMN_NUMBER = "number"; public static final String COLUMN_ISSTAR = "isstar"; private static int personDir=0;//列表 private static int personItem=1;//单条记录 // 初始化:创建UriMatcher对象,参数UriMatcher.NO_MATCH表示初始状态(无匹配)。 // 添加匹配规则:使用addURI方法,参数为授权(authority)、路径(path)和自定义代码(code)。 // 使用:通过match方法匹配传入的Uri,返回自定义代码,然后根据代码进行相应处理。 private static UriMatcher uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);//初始状态没匹配到任何URI private SQLiteDatabase dbHelper; static {//只在类被加载时执行一次 uriMatcher.addURI(AUTHORITY, "person", personDir); // 整个表,匹配返回匹配吗personDir uriMatcher.addURI(AUTHORITY, "person/#", personItem); // 单条记录,#匹配由任意长度的数字字符组成的字符串 } //uri 映射至程序公开的数据表person //projection 是检索到的每个行所应包含的列的数组。 //selection 指定选择行的条件。 //sortOrder 指定在返回的 Cursor 中各行的显示顺序。 public DatabaseProvider() { } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { // Implement this to handle requests to delete one or more rows. throw new UnsupportedOperationException("Not yet implemented"); } @Override public String getType(Uri uri) { // TODO: Implement this to handle requests for the MIME type of the data // at the given URI. throw new UnsupportedOperationException("Not yet implemented"); } @Override public Uri insert(Uri uri, ContentValues values) { // TODO: Implement this to handle requests to insert a new row. throw new UnsupportedOperationException("Not yet implemented"); } @Override public boolean onCreate() { // TODO: Implement this to initialize your content provider on startup. return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { switch (uriMatcher.match(uri)){ case 0: Log.i("test","query所有"); return dbHelper.query(false,"person",projection,selection,selectionArgs,null,null,sortOrder,null); case 1: long id= ContentUris.parseId(uri); Log.i("test","query单个"); return dbHelper.rawQuery("select * from person where id=?",new String[]{id+""}); } return null; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO: Implement this to handle requests to update one or more rows. throw new UnsupportedOperationException("Not yet implemented"); } }

资源评论
用户头像
张盛锋
2025.06.15
对于初学者来说,是一篇很好的入门级教程。🍖
用户头像
又可乐
2025.06.01
内容详实,适用于Android开发人员学习ContentUris的使用方法。
用户头像
叫我叔叔就行
2025.02.13
结合实例说明,便于理解ContentUris类的实际应用。🎉
用户头像
天使的梦魇
2025.01.31
简洁明了地展示了ContentUris类的核心功能,易于学习。
用户头像
章满莫
2025.01.30
对于3G应用开发有具体指导意义,实用性较强。
用户头像
滕扬Lance
2024.12.26
内容讲解透彻,重点突出,有助于快速掌握Uri操作技巧。
简单的暄
  • 粉丝: 28
上传资源 快速赚钱