码迷,mamicode.com
首页 > 移动开发 > 详细

Android 从网页中跳转到APP

时间:2016-05-12 18:37:47      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

先说实现步骤再说原理:


使用步骤

一,首先要给你要打开的应用中的activity设置过滤器(在清单文件里设置)
以JumpActivity为例
如下面的: <intent-filter>  中就是所需过滤器

<activity android:name=".JumpActivity" >  
    <intent-filter>  
        <action android:name="android.intent.action.VIEW" />  
  
  
        <category android:name="android.intent.category.DEFAULT" />  
        <category android:name="android.intent.category.BROWSABLE" />  
  
		<!--下面所设置的质需要和html端对调-->
		<!--在data里设置了 scheme和host,则该Activity可以接收和处理类似于 "sharetest://data/XXX"的链接-->
        <data  
            android:host="data"  
            android:scheme="sharetest" />  
    </intent-filter>  
</activity> 

 
二,在JumpActivity中做打开后的处理,用来接收外部的跳转


public class JumpActivity extends Activity{   
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
		
        Intent intent = getIntent();//在这个Activity里,我们可以通过getIntent(),来获取外部跳转传过来的信息。
        String data = intent.getDataString();//接收到网页传过来的数据:sharetest://data/http://www.huxiu.com/
        String[] split = data.split("data/");//以data/切割data字符串
        url = split[1]; //就得到:http://www.huxiu.com/(这就是我们需要网页传给我们的数据)
		。。。然后我们再通过网页打开app的同时就可以用获得的url数据做一些我们需要做的处理
		比如你在微信里浏览网页时打开自己的安卓app应用的同时,加载一个app内的网页
    }  
      
}  



三,我们需要找到html前端,让他们在网页中加入:

<iframe src="" style="display:none"></iframe>


如下:index.html

<!DOCTYPE html>  
	<html>  
	<body>  
	<iframe src="sharetest://data/http://www.huxiu.com/" style="display:none"></iframe>  
	</body>  
	</html>  

将index.html放到Assets目录下,在代码里调用Webview加载该Html文件,代码如下:


/*网页打开app*/
public class H5ToAppActivity extends Activity {
    private String url;
    private WebView webview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5_app);


        webview = (WebView) findViewById(R.id.webviewh5);
        url = "file:///android_asset/index.html";


        WebSettings wSet = webview.getSettings();
        wSet.setJavaScriptEnabled(true);
        webview.loadUrl(url);
    }
}

这样执行以上代码时就可以打开对应的app了。

比如我的2048是一个网页,打开网页的时候可以同时打开另外一个应用

下面是两个应用截图你可以下载下来看下效果:(两个应用一起下)

2048网页演示apk:http://download.csdn.net/detail/qiushi_1990/9514778

网页打开的应用apk:http://download.csdn.net/detail/qiushi_1990/9514779

技术分享

这样在打开2048时会出现下面效果

技术分享


然后会跳转到下面应用

技术分享



实现原理

最近,在使用QQ和微信等SDK来实现分享网页的时候,发现,SDK已经为页面跳转回应用提供了基本的数据支持。我们只需在应用里和被分享的网页进行简单的设置,即可实现此功能。

那么我们先来看下网页跳转回应用的实现原理。

Android平台而言,URI主要分三个部分:scheme, authority and path。其中authority又分为host和port。格式如下: 
scheme://host:port/path 
举个实际的例子: 
content://com.example.project:200/folder/subfolder/etc 
\---------/  \---------------------------/ \---/ \--------------------------/ 
scheme                 host               port        path 
                \--------------------------------/ 
                          authority    

现在大家应该知道data flag中那些属性的含义了吧,看下data flag 
<data android:host="string" 
      android:mimeType="string" 
      android:path="string" 
      android:pathPattern="string" 
      android:pathPrefix="string" 
      android:port="string" 
      android:scheme="string" /> 

点击微信和QQ分享跳转到程序内部的原理与此一致。




































Android 从网页中跳转到APP

标签:

原文地址:http://blog.csdn.net/qiushi_1990/article/details/51355073

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!