Allowing OtherApps to Start Your Activity
Android我们在使用资源的使用总会设计到URL,URI。发现都能够具体的定位到一个东西,就查了查他们两者有什么区别捏?
同样的,另外一个问题:String HttpServletRequest、getRequestURI();和StringBuffer HttpServletRequest、getRequestURL();返回的内容有何不同?为什么会如此?
看了一下JavaDoc知道。
URIs, URLs, and URNs首先,URI,是uniform本内容首发于55ab社区 resource identifier,统一资源标识符,用来唯一的标识一个资源。而URL是uniform resource locator,统一资源定位器,它是一种具体的URI,即URL可以用来标识一个资源,而且还指明了如何locate这个资源。而URN,uniform resource name,统一资源命名,是通过名字来标识资源,比如mailto:java-net@java。也就是说,URI是以一种抽象的,高层次概念定义统一资源标识,而URL和URN则是具体的资源标识的方式。URL和URN都是一种URI。
在Java的URI中,一个URI实例可以代表绝对的,也可以是相对的,只要它符合URI的语法规则。而URL类则不仅符合语义,还包含了定位该资源的信息,因此它不能是相对的,schema必须被指定。
为了开发更多人使用的App,我们总希望我们的App能够提供一种接口被其他App调用。如我们常见的 大众点评与豆瓣。他们这种资源丰富的App能给我们提供很多丰富的资源。
例如豆瓣的AndroidManifext.xml中的 scheme:
<activity android:name="com.douban.movie.PlayVideoActivity" > <intent-filter> <action android:name="com.douban.movie" > </action> <action android:name="android.intent.action.VIEW" > </action> <category android:name="android.intent.category.DEFAULT" > </category> <category android:name="android.intent.category.BROWSABLE" > </category> <data android:host="movie.douban.com" android:pathPattern="/trailer/.*/" android:scheme="http" > </data> </intent-filter> </activity>
主要的是在于定义了:
android:name="android.intent.category.BROWSABLE" <data android:scheme="http" android:host="movie.douban.com" android:pathPattern="/trailer/.*/" > </data> <data android:scheme="http" android:host="movie.douban.com" android:pathPattern="/trailer/.*/" > </data>
我们知道,如果用户的手机上没有安装您的App,第三方App如果需要使用Scheme跳转的话就会产生错误。
这个样子的话我们的一般解决办法是直接跳转到网页版的应用上去。
所以,将Scheme写成类似Url的形式方便我们进行应用内的跳转与网页上的跳转。
当然,也可以分开来写,如同大众点评的。
String id = "3102397"; try { Uri url = Uri.parse("dianping://shopinfo?id=" + id); Intent intent = new Intent(Intent.ACTION_VIEW, url); mContext.startActivity(intent); } catch (Exception e) { // 没有安装应用,默认打开HTML5站 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.dianping.com/shop/" + id)); mContext.startActivity(intent); }String id = "3102397"; try { Uri url = Uri.parse("dianping://shopinfo?id=" + id); Intent intent = new Intent(Intent.ACTION_VIEW, url); mContext.startActivity(intent); } catch (Exception e) { // 没有安装应用,默认打开HTML5站 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.dianping.com/shop/" + id)); mContext.startActivity(intent); }
当然,进入Activity之后,处理Uri类,获取path,就能够 做相应的参数传递,跳转到不同的界面了
Uri uri = getIntent().getData(); uri.getPath();
附件中上传了一个自己写的sheme的demo如果不清楚的可以看看
demo http://download.csdn.net/detail/yzzst/6216457