Android URI&&URL的定义与使用

URI URL URN的原有英文与解释:

URI (uniform resource identifier)统一资源标志符;
URL(uniform resource location )统一资源定位符(或统一资源定位器);

URN(uniform resource name )统一资源命名。

什么是URL:
URL是internet上用来描述信息资源文件的字符串,用在客户程序和服务器上,定位客户端连接服务器所需要的信息,它不仅定位了这个信息资源,而且定义了如何找到这个资源。

什么是URI:
URI是一个相对来说更广泛的概念,URL是URI的一种,是URI命名机制的一个子集,可以说URI是抽象的,而具体要使用URL来定位资源。web上的每一种资源如:图片、文档、视频等,都是由URI定位的,这里所谓的定位指的是web上的资源相对于主机服务器来说,存放在服务器上的具体路径。
URI一般由三部分组成:

访问资源的命名机制。

存放资源的主机名。

资源自身的名称。如下图所示:

Android ContentResolver使用说明(1)_27881
将其分为A,B,C,D 4个部分:
A:标准前缀,用来说明一个Content Provider控制这些数据,无法改变的;
B:URI的标识,它定义了是哪个Content Provider提供这些数据。对于第三方应用程序,为了保证URI标识的唯一性,它必须是一个完整的、小写的  类名。这个标识在<provider> 元素的 authorities属性中说明:
<provider name=”.TransportationProvider”  authorities=”com.example.transportationprovider”  . . .  >
C:路径,Content Provider使用这些路径来确定当前需要生什么类型的数据,URI中可能不包括路径,也可能包括多个;
D:如果URI中包含,表示需要获取的记录的ID;如果没有ID,就表示返回全部;
由于URI通常比较长,而且有时候容易出错,切难以理解。所以,在Android当中定义了一些辅助类,并且定义了一些常量来代替这些长字符串,例如:People.CONTENT_URI。
同时Android系统还提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris。UriMatcher主要用于匹配URI,ContentUris主要用于获取Uri路径后面的ID部分。

下面列举一些Android系统中常用的Uri例子:
显示网页:
[java]  view plain  copy
  1. Uri uri = Uri.parse("http://www.google.com");   
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);   
  3. startActivity(it);   
显示地图:
[java]  view plain  copy
  1. Uri uri = Uri.parse("geo:38.899533, -77.036476");   
  2. Intent it = new Intent(Intent.Action_VIEW, uri);   
  3. startActivity(it);   
路径规划:
[java]  view plain  copy
  1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat startLng&daddr=endLat endLng&hl=en");   
  2. Intent it = new Intent(Intent.ACTION_VIEW, URI);   
  3. startActivity(it);   
调用拨号程序:
[java]  view plain  copy
  1. Uri uri = Uri.parse("tel:xxxxxx");   
  2. Intent it = new Intent(Intent.ACTION_DIAL, uri);     
  3. startActivity(it);     
  4.   
  5. Uri uri = Uri.parse("tel.xxxxxx");   
  6. Intent it =new Intent(Intent.ACTION_CALL,uri);   
  7.   
  8. <uses-permission id="Android.permission.CALL_PHONE" />   
调用发送短信/彩信的程序:
[java]  view plain  copy
  1. Intent it = new Intent(Intent.ACTION_VIEW);   
  2. it.putExtra("sms_body""The SMS text");   
  3. it.setType("vnd.android-dir/mms-sms");   
  4. startActivity(it);     
发送短信:
[java]  view plain  copy
  1. Uri uri = Uri.parse("smsto:0800000123");   
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
  3. it.putExtra("sms_body""The SMS text");   
  4. startActivity(it);     
发送彩信:
[java]  view plain  copy
  1. Uri uri = Uri.parse("content://media/external/images/media/23");   
  2. Intent it = new Intent(Intent.ACTION_SEND);   
  3. it.putExtra("sms_body""some text");   
  4. it.putExtra(Intent.EXTRA_STREAM, uri);   
  5. it.setType("image/png");   
  6. startActivity(it);   
发送EMail:
[java]  view plain  copy
  1. Uri uri = Uri.parse("mailto:xxx@abc.com");   
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
  3. startActivity(it);   
  4.   
  5. Intent it = new Intent(Intent.ACTION_SEND);   
  6. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");   
  7. it.putExtra(Intent.EXTRA_TEXT, "The email body text");   
  8. it.setType("text/plain");   
  9. startActivity(Intent.createChooser(it, "Choose Email Client"));     
  10.   
  11. Intent it=new Intent(Intent.ACTION_SEND);     
  12. String[] tos={"me@abc.com"};     
  13. String[] ccs={"you@abc.com"};     
  14. it.putExtra(Intent.EXTRA_EMAIL, tos);     
  15. it.putExtra(Intent.EXTRA_CC, ccs);     
  16. it.putExtra(Intent.EXTRA_TEXT, "The email body text");     
  17. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");     
  18. it.setType("message/rfc822");     
  19. startActivity(Intent.createChooser(it, "Choose Email Client"));   
添加附件:
[java]  view plain  copy
  1. Intent it = new Intent(Intent.ACTION_SEND);   
  2. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
  3. it.putExtra(Intent.EXTRA_STREAM, "[url=]file:///sdcard/mysong.mp3[/url]");   
  4. sendIntent.setType("audio/mp3");   
  5. startActivity(Intent.createChooser(it, "Choose Email Client"));   
播放多媒体:
[java]  view plain  copy
  1. Intent it = new Intent(Intent.ACTION_VIEW);   
  2. Uri uri = Uri.parse("[url=]file:///sdcard/song.mp3[/url]");   
  3. it.setDataAndType(uri, "audio/mp3");   
  4. startActivity(it);   
  5.   
  6. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   
  7. Intent it = new Intent(Intent.ACTION_VIEW, uri);   
  8. startActivity(it);     
Uninstall APK
[java]  view plain  copy
  1. Uri uri = Uri.fromParts("package", strPackageName, null);   
  2. Intent it = new Intent(Intent.ACTION_DELETE, uri);   
  3. startActivity(it);   
调用相册:
[java]  view plain  copy
  1. public static final String MIME_TYPE_IMAGE_JPEG = "image   
  2. Uri packageURI = Uri.parse("package:"+wistatmap);     
  3. Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);     
  4. startActivity(uninstallIntent);   
Install APK:
[java]  view plain  copy
  1. Uri installUri = Uri.fromParts("package""xxx"null);   
  2. returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);   
  3. play audio   
  4. Uri playUri = Uri.parse("[url=]file:///sdcard/download/everything.mp3[/url]");   
  5. returnIt = new Intent(Intent.ACTION_VIEW, playUri);   
搜索应用:
[java]  view plain  copy
  1. Uri uri = Uri.parse("market://search?q=pname:pkg_name");     
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);     
  3. startActivity(it);     
  4. //where pkg_name is the full package path for an application   
进入联系人页面:
[java]  view plain  copy
  1. Intent intent = new Intent();   
  2. intent.setAction(Intent.ACTION_VIEW);   
  3. intent.setData(People.CONTENT_URI);   
  4. startActivity(intent);   
查看制定联系人:
[java]  view plain  copy
  1. Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, info.id);//info.id联系人ID   
  2. Intent intent = new Intent();   
  3. intent.setAction(Intent.ACTION_VIEW);   
  4. intent.setData(personUri);   
  5. startActivity(intent);   
0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值