android 网页中打开 app指定页面

一、网页打开app简介

 1. 用户自定义的URI (Custom URI scheme)  形式如下:

        scheme://host/path?parameters

 2. "intent:"语法(intent -base URI) 语法形式如下:

        intent://host#intent;参数;end

二、自定义 Custom Scheme URI  

1.  我们先创建一个网页HTMl

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>网页打开App</title>
</head>
<body>
    <a href="app://app.yuong.com?load_url=www.yuong.com">打开app</a>
</body>
</html>

注 :     在 app://app.yuong.com?load_url=www.yuong.com  中

             app是 scheme     

             app.yuong.com 是 host  

             load_url=www.yuong.com  是我们需要传递的参数(key和value)

2. 我们创建一个android  project   新建一个WebJumpActivity,在AndroidManifest.xml中定义WebJumpActivity的属性,如下:

<activity android:name=".WebJumpActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="app.yuong.com"
            android:scheme="app" />
    </intent-filter>
</activity>

3.  在WebJumpActivity的 onCreate()方法中获取传递的参数

Intent intent = getIntent();
Uri uri = intent.getData();
if (uri != null) {
    Log.e(TAG, "url : " + uri.toString());
    String value = uri.getQueryParameter("load_url");
    Log.e(TAG, "load_url : " + value);
}

这样通过网页点击链接就可以直接打开我们的app WebJumpActivity界面

二、Intent -based URl  

 Intent-based URI语法:

1. 首先我们新建一个HTML ,如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>网页打开App</title>
</head>
<body>
  <a href="intent:app://app.yuong.com#Intent;action=android.intent.action.VIEW;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;component=com.yuong.demo/.MainActivity;S.load_url=www.yuong.com;end">打开app</a>
</body>
</html>

2.  按Intent-based URI的语法创建链接,我们可以在app项目中通过以下代码:

Intent intent=new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addCategory("android.intent.category.BROWSABLE");
intent.setData(Uri.parse("app://app.yuong.com"));
intent.putExtra("load_url","www.yuong.com");
Log.e(TAG,"URL : "+intent.toURI());
startActivity(intent);


得到日志:app://app.yuong.com#Intent;action=android.intent.action.VIEW;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;component=com.yuong.demo/.MainActivity;S.load_url=www.yuong.com;end  最后在前面加上“intent:”(不能忘)

3.  在WebJumpActivity的 onCreate()方法中获取传递的参数

Intent intent = getIntent();
Log.e(TAG, "intent base uri  : " + intent.toURI());
Log.e(TAG, "data : " + intent.getDataString());
Log.e(TAG, "is exist  load_url : " + intent.hasExtra("load_url"));
Log.e(TAG, "load_url : " + intent.getStringExtra("load_url"));

这样通过这种方式也可以打开我们的app WebJumpActivity,相对第一种复杂一些

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值