当前位置:操作系统 > 安卓/Android >>

移动开发中的Scheme跳转说明——Allowing OtherApps to Start Your Activity

Allowing OtherApps to Start Your Activity
 
为了开发更多人使用的App,我们总希望我们的App能够提供一种接口被其他App调用。如我们常见的 大众点评  与  豆瓣。他们这种资源丰富的App能给我们提供很多丰富的资源。
例如豆瓣的scheme:
                 
[html]  
<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:scheme="http"  
                                 android:host="movie.douban.com"  
                                 android:pathPattern="/trailer/.*/"  
                                 >  
                       </data>  
              </intent-filter>  
     </activity>  
 
 
主要的是在于定义了:
[html] 
android:name="android.intent.category.BROWSABLE"  
 
 
         
[html] 
<data  
     android:scheme="http"  
     android:host="movie.douban.com"  
     android:pathPattern="/trailer/.*/"  
>  
</data>  
 
我们知道,如果用户的手机上没有安装您的App,第三方App如果需要使用Scheme跳转的话就会产生错误。
这个样子的话我们的一般解决办法是直接跳转到网页版的应用上去。
所以,将Scheme写成类似Url的形式方便我们进行应用内的跳转与网页上的跳转。
 
当然,也可以分开来写,如同大众点评的。
[java] 
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);  
                   }  
 
 
 
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,