今天学习关于android的URI的学习,


一、使用地点

通用资源标志符(Universal Resource Identifier, 简称"URI")。

Uri代表要操作的数据,Android上可用的每种资源 - 图像、视频片段等都可以用Uri来表示。

URI一般由三部分组成:

访问资源的命名机制。

存放资源的主机名。

资源自身的名称,由路径表示。

Android的Uri由以下三部分组成: "content://"、数据的路径、标示ID(可选)

举些例子,如:

所有联系人的Uri: content://contacts/people

某个联系人的Uri: content://contacts/people/5

所有图片Uri: content://media/external

某个图片的Uri:content://media/external/images/media/4

我们很经常需要解析Uri,并从Uri中获取数据。

Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris。

虽然这两类不是非常重要,但是掌握它们的使用,会便于我们的开发工作。

下面就一起看一下这两个类的作用。



二、在INTENT中作为参数传递

1、打开一个网页,类别是Intent.ACTION_VIEW

?
1 2 Uri uri = Uri.parse( "http://www.android-study.com/" ); Intent intent = new Intent(Intent.ACTION_VIEW, uri);

2二、打开地图并定位到一个点

?
1 2 Uri uri = Uri.parse( "geo:52.76,-79.0342" ); Intent intent = new Intent(Intent.ACTION_VIEW, uri);

3、打开拨号界面,类型是Intent.ACTION_DIAL

?
1 2 Uri uri = Uri.parse( "tel:10086" ); Intent intent = new Intent(Intent.ACTION_DIAL, uri);

4、直接拨打电话,与三不同的是,这个直接拨打电话,而不是打开拨号界面

?
1 2 Uri uri = Uri.parse( "tel:10086" ); Intent intent = new Intent(Intent.ACTION_CALL, uri);

5、卸载一个应用,Intent的类别是Intent.ACTION_DELETE

?
1 2 Uri uri = Uri.fromParts( "package" , "xxx" , null ); Intent intent = new Intent(Intent.ACTION_DELETE, uri);

6、安装应用程序,Intent的类别是Intent.ACTION_PACKAGE_ADDED

?
1 2 Uri uri = Uri.fromParts( "package" , "xxx" , null ); Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, uri);

7、播放音频文件

?
1 2 3 Uri uri = Uri.parse( "file:///sdcard/download/everything.mp3" ); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.setType( "audio/mp3" );

8、打开发邮件界面

?
1 2 Uri uri= Uri.parse( "mailto:admin@android-study.com" ); Intent intent = new Intent(Intent.ACTION_SENDTO, uri);

9、发邮件,与八不同这里是将邮件发送出去

?
1 2 3 4 5 6 7 8 Intent intent = new Intent(Intent.ACTION_SEND); String[] tos = { "admin@android-study.com" }; String[] ccs = { "webmaster@android-study.com" }; intent.putExtra(Intent.EXTRA_EMAIL, tos); intent.putExtra(Intent.EXTRA_CC, ccs); intent.putExtra(Intent.EXTRA_TEXT, "I come fromhttp://www.android-study.com" ); intent.putExtra(Intent.EXTRA_SUBJECT, "http://www.android-study.com" );intent.setType( "message/rfc882" ); Intent.createChooser(intent, "Choose Email Client" );

//发送带附件的邮件

?
1 2 3 4 5 Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text" ); intent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3" ); intent.setType( "audio/mp3" ); startActivity(Intent.createChooser(intent, "Choose Email Client" ));

10、发短信

?
1 2 3 4 Uri uri= Uri.parse( "tel:10086" ); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.putExtra( "sms_body" , "I come fromhttp://www.android-study.com" ); intent.setType( "vnd.Android-dir/mms-sms" );

11、直接发短信

?
1 2 3 Uri uri= Uri.parse( "smsto://100861" ); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra( "sms_body" , "3g androidhttp://www.android-study.com" );

12、发彩信

?
1 2 3 4 5 Uri uri= Uri.parse( "content://media/external/images/media/23" ); Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra( "sms_body" , "3g androidhttp://www.android-study.com" ); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.setType( "image/png" );

13、# Market 相关

?
1 2 3 4 5 6 7 8 9 10 11 12 13 1 //寻找某个应用 Uri uri = Uri.parse( "market://search?q=pname:pkg_name" ); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); //where pkg_name is the full package path for an application 2 //显示某个应用的相关信息 Uri uri = Uri.parse( "market://details?id=app_id" ); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); //where app_id is the application ID, find the ID //by clicking on your application on Market home //page, and notice the ID from the address bar

14、路径规划

?
1 2 3 4 Uri uri = Uri.parse( "http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en" ); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456

15、安装指定apk

?
1 2 3 4 5 6 public void setupAPK(String apkname){ String fileName = Environment.getExternalStorageDirectory() + "/" + apkname; Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile( new File(fileName)), "application/vnd.android.package-archive" ); mService.startActivity(intent); }

16、进入联系人页面

?
1 2 3 4 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(People.CONTENT_URI); startActivity(intent);

17、查看指定联系人

?
1 2 3 4 5 Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, info.id); // info.id联系人ID Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(personUri); startActivity(intent);

18、调用相册

?
1 2 3 4 5 6 public static final String MIME_TYPE_IMAGE_JPEG = "image/*" ; public static final int ACTIVITY_GET_IMAGE = 0 ; Intent getImage = new Intent(Intent.ACTION_GET_CONTENT); getImage.addCategory(Intent.CATEGORY_OPENABLE); getImage.setType(MIME_TYPE_IMAGE_JPEG); startActivityForResult(getImage, ACTIVITY_GET_IMAGE);

19、调用系统相机应用程序,并存储拍下来的照片

?
1 2 3 4 5 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); time = Calendar.getInstance().getTimeInMillis(); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile( new File(Environment .getExternalStorageDirectory().getAbsolutePath()+ "/tucue" , time + ".jpg" ))); startActivityForResult(intent, ACTIVITY_GET_CAMERA_IMAGE)

android中的UriMatcher和ContentUris

用于操作Uri的工具类,分别为UriMatcher 和ContentUris。
UriMatcher
用于匹配Uri。
用法如下:
首先把你需要匹配Uri路径全部给注册上:
注册完需要匹配的Uri后,就可以使用sMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配码是调用 addURI()方法传入的第三个参数,例如匹配content://com.test.provider.personprovider/person 路径,返回的匹配码为1。

//常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码
UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

//添加需要匹配的uri,如果匹配就会返回匹配码
//如果match()方法匹配content://com.test.provider.personprovider/person路径,返回匹配码为1
sUriMatcher.addURI(“com.test.provider.personprovider”, “person”, 1);

//如果match()方法匹配content://com.test.provider.personprovider/person/530路径,返回匹配码为2
//#号为通配符
sUriMatcher.addURI(“com.test.provider.personprovider”, “person/#”, 2);

switch(sUriMatcher.match(Uri.parse("content://com.test.provider.personprovider/person/10"))) {
case 1 break;
case 2 break;
default:
//不匹配 break;
}


ContentUris类
用于获取Uri路径后面的ID部分:
1、ContentUris.withAppendedId(Uri contentUri, long id)用于为路径加上ID部分:
Uri uri = Uri.parse("content://com.test.provider.personprovider/person")
Uri resultUri = ContentUris.withAppendedId(uri, 5);
//生成后的Uri为:content://com.test.provider.personprovider/person/5

其结果等价于Uri.withAppendedPath(Uri baseUri, String pathSegment)
Uri resultUri = Uri.withAppendedPath(uri, "5");

2、ContentUris.parseId(uri)方法用于从路径中获取ID部分:
Uri uri = Uri.parse("content://com.test.provider.personprovider/person/5")
long personid = ContentUris.parseId(uri);
//获取的结果为:5


ContentResolver
通过URI来添加、删除、修改和查询ContentProvider中提供的数据。除了URI以外,还必须知道需要获取的数据段的名称,以及此数据段的数据类型。如果你需要获取一个特定的记录,你就必须知道当前记录的ID,也就是URI中D部分。
可以使用Activity提供的getContentResolver()方法。
ContentResolver使用insert、delete、update、query方法来操作数据。
1、final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)Query the given URI, returning a Cursor over the result set.
2、final Uri insert(Uri url, ContentValues values)Inserts a row into a table at the given URL.
3、final int update(Uri uri, ContentValues values, String where, String[] selectionArgs)Update row(s) in a content URI.
4、final int delete(Uri url, String where, String[] selectionArgs)Deletes row(s) specified by a content URI.






更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. Pycharm安装PyQt5的详细教程
  3. python起点网月票榜字体反爬案例
  4. mulator: ERROR: no search paths found in this AVD's configur
  5. android 连接远程数据库
  6. 开发自己的监控系统三、移动篇(android)
  7. Android[中级教程]第八章 Json数据的处理
  8. 在Android中使用ORMLite
  9. Android串口通讯开发整理

随机推荐

  1. Xhorse Key Tool Plus所有丢失的密钥添加
  2. 中断时间序列分析ITSA是什么? 很流行的政
  3. 趁火打劫!印度APT组织对我国医疗机构发起
  4. 用PowerShell通过Posh-SSH免密码自动登录
  5. 倡议: 你来推荐文献, 我来帮你解读, 让计
  6. 自动驾驶思考:硬件篇
  7. 推荐系统:石器与青铜时代
  8. 回放:如何构建高效能的地图定位系统
  9. 自动驾驶思考:基础架构篇
  10. 闭包函数及其应用