您的位置:首页 > 其它

解锁Activity的跳转新姿势———使用scheme跳转

2018-02-05 23:50 357 查看

Activity的跳转可以说是非常简单的了、从一个页面跳转至另一个页面,我们最常用的也就是下面这种:

Intent intent = new Intent(this, xxx.class);
startActivity(intent);
1
2
[/code]

当然还有我们的隐式跳转,为Intent指定一个
action
即可

Intent intent = new Intent("this is an action");
startActivity(intent);
1
2
[/code]

这篇文章上面提到的两个都不讲,这里来说使用scheme协议来进行页面跳转

manifest / data 配置

如果需要让我们的Activity能被其他应用或者网页所打开,需要在
AndroidManifest
中进行配置(浏览网页的时候点击一些广告可以进对应的app大概就是这个原理)

<activity android:name=".TestOneActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!--配置scheme-->
<data
android:scheme="azhon.scheme" />
</intent-filter>
</activity>
1
2
3
4
5
6
7
8
9
10
[/code]

如上代码我们只是简单的配置了一个
android:scheme
属性,那要打开这个Activity也是so easy的

startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("azhon.scheme://")));
1
2
[/code]

当然< data/>中还可以配置更多的属性(截取自官方中文文档,可以点击上面的manifest / data 配置)

<data android:scheme="string"
android:host="string"
android:port="string"
android:path="string"
android:pathPattern="string"
android:pathPrefix="string"
android:mimeType="string" />
1
2
3
4
5
6
7
[/code]

scheme 路径的规则

<scheme> :// <host> : <port> [<path>|<pathPrefix>|<pathPattern>]
1
[/code]

scheme开头 :// 主机名 : 端口号 / [路径/参数] 路径后面拼接参数也是可以的下面会用到。

这个地址和我们经常使用的网页地址差不多是一样滴

eg:https://loaclhost:8080/index.jsp

在网页中打开我们对应的页面

我们修改Manifest中Activity的配置在多加点料,我这里创建了两个页面

<activity android:name=".TestOneActivity">
<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="azhon"
android:port="1011"
android:scheme="azhon.scheme" />
</intent-filter>
</activity>

<activity android:name=".TestTwoActivity">
<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="azhon"
android:port="1012"
android:scheme="azhon.scheme" />
</intent-filter>
</activity>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[/code]

在主页面布局添加一个WebView控件,并让它加载
assets
文件夹中的html文件

布局文件代码:

<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
1
2
3
4
5
[/code]

assets文件夹中的Html代码:

<a href="azhon.scheme://azhon:1011/我是路径?user=958460248&psd=123456">跳转至TestOneActivity</a>
<p>
<a href="azhon.scheme://azhon:1012/i am path?toId=25&tmId=888">跳转至TestTwoActivity</a>
1
2
3
[/code]

效果图:



在网页中只需要使用一个超链接便可以轻松的跳转至我们的app,是不是很开心。

当你将这个html文件使用手机的浏览器查看时,点击也是一样可以跳转进app的

跳转页面的时候还可以通过 ?key=value&key1=value2 键值对的方式往地址后面拼接参数

在跳转过去的页面接收传递过来的参数

Intent intent = getIntent();
Uri uri = intent.getData();

assert uri != null;
sb.append("scheme: ").append(intent.getScheme()).append("<p>");
sb.append("host: ").append(uri.getHost()).append("<p>");
sb.append("port: ").append(uri.getPort()).append("<p>");
sb.append("path: ").append(uri.getPath()).append("<p>");
sb.append("params: ").append(uri.getQuery()).append("<p>");

//获取跳转过来携带所有参数的 键名
Set<String> names = uri.getQueryParameterNames();
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
sb.append(key).append(": ").append(uri.getQueryParameter(key)).append("<p>");
}
tv.setText(Html.fromHtml(sb.toString()));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[/code]

有了这种跳转方式 你只要知道其他app的scheme你就可以随意的跳过去了想想都刺激,比如下面代码打开微信和QQ程序。

<a href="weixin://">打开微信</a>
<p>
<a href="mqqzone://">打开QQ</a>
1
2
3
[/code]

效果图:



这里提供了Demo下载

小技巧:查看其他app在Manifest中声明的scheme地址,这里分享一个教程

如果你使用的是Android Studio2.4或者更高版本你可以直接将apk拖进Android Studio即可查看。

查看APK中AndroidManifest.xml内容

转自:http://blog.csdn.net/a_zhon/article/details/78358310
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐