URI & Uri
Uri扩展了Java中URI的一些功能以适用于Android开发,其中包括将Serializable改为了Parcelable
public abstract class Uri implements Comparable<Uri>, Parcelable { }
public final class URI implements Comparable<URI>, Serializable { }
Uri格式
[scheme:]scheme-specific-part[#fragment]
[scheme:][//authority][path][?query][#fragment]
[scheme:][//host:port][path][?query][#fragment]
scheme://authority/path1/path2/path3?id = 1&name = mingming&old#fragment
相关方法
以http://www.java2s.com:8080/yourpath/fileName.htm?stove=10&path=32&id=4#harvic为例:
getScheme():http
getSchemeSpecificPart()://www.java2s.com:8080/yourpath/fileName.htm?
getFragment():harvic
getAuthority():www.java2s.com:8080
getPath():/yourpath/fileName.htm
getQuery():stove=10&path=32&id=4
getHost():www.java2s.com
getPost():8080
另外还有两个常用方法:getPathSegments()、getQueryParameter(String key)
List<String> getPathSegments()的作用就是依次提取出Path的各个部分的字符串,以字符串数组的形式输出
getQueryParameter(String key)的作用就是通过传进去path中某个Key的字符串,返回他对应的值
常用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%20startLng&daddr=endLat%20endLng&hl=en");
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用法
<intent-filter> <!--正常启动-->
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter> <!--URL Scheme启动-->
<!--必有项-->
<action android:name="android.intent.action.VIEW" />
<!--如果希望该应用可以通过浏览器的连接启动,则添加该项-->
<category android:name="android.intent.category.BROWSABLE" />
<!--表示该页面可以被隐式调用,必须加上该项-->
<category android:name="android.intent.category.DEFAULT" />
<!--协议部分-->
<!--组成URI的是可选的,但存在如下的依赖关系:
如果没有指定scheme,那么host参数会被忽略
如果没有指定host,那么port参数会被忽略
如果scheme和host都没有指定,path参数会被忽略 -->
<data android:scheme="url_scheme"
android:host="auth_activity"
android:mimeType="abc/xyz"/>
</intent-filter>
<intent-filter>
<action android:name="emms.intent.action.check_authorization" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="emms.intent.category.authorization" />
</intent-filter>
</activity>