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一般由三部分组成:
访问资源的命名机制。
存放资源的主机名。
资源自身的名称。如下图所示:
将其分为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例子:
显示网页:
- Uri uri = Uri.parse("http://www.google.com");
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(it);
显示地图:
- Uri uri = Uri.parse("geo:38.899533, -77.036476");
- Intent it = new Intent(Intent.Action_VIEW, uri);
- startActivity(it);
路径规划:
- Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat startLng&daddr=endLat endLng&hl=en");
- Intent it = new Intent(Intent.ACTION_VIEW, URI);
- startActivity(it);
调用拨号程序:
- Uri uri = Uri.parse("tel:xxxxxx");
- Intent it = new Intent(Intent.ACTION_DIAL, uri);
- startActivity(it);
-
- Uri uri = Uri.parse("tel.xxxxxx");
- Intent it =new Intent(Intent.ACTION_CALL,uri);
-
- <uses-permission id="Android.permission.CALL_PHONE" />
调用发送短信/彩信的程序:
- Intent it = new Intent(Intent.ACTION_VIEW);
- it.putExtra("sms_body", "The SMS text");
- it.setType("vnd.android-dir/mms-sms");
- startActivity(it);
发送短信:
- Uri uri = Uri.parse("smsto:0800000123");
- Intent it = new Intent(Intent.ACTION_SENDTO, uri);
- it.putExtra("sms_body", "The SMS text");
- startActivity(it);
发送彩信:
- Uri uri = Uri.parse("content://media/external/images/media/23");
- Intent it = new Intent(Intent.ACTION_SEND);
- it.putExtra("sms_body", "some text");
- it.putExtra(Intent.EXTRA_STREAM, uri);
- it.setType("image/png");
- startActivity(it);
发送EMail:
- Uri uri = Uri.parse("mailto:xxx@abc.com");
- Intent it = new Intent(Intent.ACTION_SENDTO, uri);
- startActivity(it);
-
- Intent it = new Intent(Intent.ACTION_SEND);
- it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
- it.putExtra(Intent.EXTRA_TEXT, "The email body text");
- it.setType("text/plain");
- startActivity(Intent.createChooser(it, "Choose Email Client"));
-
- Intent it=new Intent(Intent.ACTION_SEND);
- String[] tos={"me@abc.com"};
- String[] ccs={"you@abc.com"};
- it.putExtra(Intent.EXTRA_EMAIL, tos);
- it.putExtra(Intent.EXTRA_CC, ccs);
- it.putExtra(Intent.EXTRA_TEXT, "The email body text");
- it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
- it.setType("message/rfc822");
- startActivity(Intent.createChooser(it, "Choose Email Client"));
添加附件:
- Intent it = new Intent(Intent.ACTION_SEND);
- it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
- it.putExtra(Intent.EXTRA_STREAM, "[url=]file:///sdcard/mysong.mp3[/url]");
- sendIntent.setType("audio/mp3");
- startActivity(Intent.createChooser(it, "Choose Email Client"));
播放多媒体:
- Intent it = new Intent(Intent.ACTION_VIEW);
- Uri uri = Uri.parse("[url=]file:///sdcard/song.mp3[/url]");
- it.setDataAndType(uri, "audio/mp3");
- startActivity(it);
-
- Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(it);
Uninstall APK
- Uri uri = Uri.fromParts("package", strPackageName, null);
- Intent it = new Intent(Intent.ACTION_DELETE, uri);
- startActivity(it);
调用相册:
- public static final String MIME_TYPE_IMAGE_JPEG = "image
- Uri packageURI = Uri.parse("package:"+wistatmap);
- Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
- startActivity(uninstallIntent);
Install APK:
- Uri installUri = Uri.fromParts("package", "xxx", null);
- returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
- play audio
- Uri playUri = Uri.parse("[url=]file:///sdcard/download/everything.mp3[/url]");
- returnIt = new Intent(Intent.ACTION_VIEW, playUri);
搜索应用:
- Uri uri = Uri.parse("market://search?q=pname:pkg_name");
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(it);
-
进入联系人页面:
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_VIEW);
- intent.setData(People.CONTENT_URI);
- startActivity(intent);
查看制定联系人:
- Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, info.id);
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_VIEW);
- intent.setData(personUri);
- startActivity(intent);