Android 从网页中跳转到APP,从微信打开自己的app并打开指定页面


<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前端,让他们在网页中加入:

如下: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” />

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值