前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Activity跳转之scheme跳转法

Activity跳转之scheme跳转法

作者头像
乱码三千
发布2021-07-29 15:36:18
1.8K0
发布2021-07-29 15:36:18
举报
文章被收录于专栏:乱码三千乱码三千

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

效果图:

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-12-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 乱码三千 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档