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

Activity的跳转可以说是非常简单的了、从一个页面跳转至另一个页面,我们最常用的也就是下面这种:
Intent intent = new Intent(this, xxx.class);
startActivity(intent);
当然还有我们的隐式跳转,为Intent指定一个action即可
Intent intent = new Intent("this is an action");
startActivity(intent);
这篇文章上面提到的两个都不讲,这里来说使用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>
  • 如上代码我们只是简单的配置了一个android:scheme属性,那要打开这个Activity也是so easy的
startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("azhon.scheme://")));
  • 当然< 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" />
scheme 路径的规则
<scheme> :// <host> : <port> [<path>|<pathPrefix>|<pathPattern>]

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>
  • 在主页面布局添加一个WebView控件,并让它加载assets文件夹中的html文件
  • 布局文件代码:
<WebView
    android:id="@+id/web"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp" />
  • 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>

效果图:
这里写图片描述

在网页中只需要使用一个超链接便可以轻松的跳转至我们的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()));
有了这种跳转方式 你只要知道其他app的scheme你就可以随意的跳过去了想想都刺激,比如下面代码打开微信和QQ程序。
    <a href="weixin://">打开微信</a>
<p>
    <a href="mqqzone://">打开QQ</a>

效果图:

这里提供了Demo下载
小技巧:查看其他app在Manifest中声明的scheme地址,这里分享一个教程
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code-Porter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值