android窗口中打开app,通过Scheme协议打开APP界面

一、应用场景

用户在访问我们的网页时,判断出这个用户手机上是否安装了我们的App,如果安装了则直接从网页上打开APP,否则就引导用户前往下载,从而形成一个推广上的闭环。这里只针对从网页端打开本地APP。

二、APP端配置

android:name=".ui.activity.ZMCertTestActivity"

android:label="@string/app_name"

android:launchMode="singleTask"

android:screenOrientation="portrait">

android:scheme="scheme1"

android:host="host1"

android:path="/path1"

android:port="8080" />

WEB端通过调用“scheme1://host1:8080/path1?query1=1&query2=true“便能打开这个Activity。其中scheme和host是必须的,另外的看需求。

2.1 APP端获取URL Scheme中的参数值

Uri uri = getIntent().getData();

if (uri != null) {

// 完整的url信息

String url = uri.toString();

Log.i(TAG, "url:" + uri);

// scheme部分

String scheme = uri.getScheme();

Log.i(TAG, "scheme:" + scheme);

// host部分

String host = uri.getHost();

Log.i(TAG, "host:" + host);

// port部分

int port = uri.getPort();

Log.i(TAG, "port:" + port);

// 访问路劲

String path = uri.getPath();

Log.i(TAG, "path:" + path);

List pathSegments = uri.getPathSegments();

// Query部分

String query = uri.getQuery();

Log.i(TAG, "query:" + query);

//获取指定参数值

String success = uri.getQueryParameter("success");

Log.i(TAG, "success:" + success);

}

三、通过WEB端打开

test

打开APP

核心就是一段Schema协议的URL,scheme1、host1是打开APP页面所必须的。传递的参数都可以在APP页面中获取到。

四、通过另一个APP打开

Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("scheme1://host1:8080/path1?query1=1&query2=true"));

startActivity(intent);

可以try catch一下,出现Exception说明手机没有安装想打开的APP。

五、后续步骤

通过Scheme协议打开APP时,又存在两种情况,即APP当前是否已经打开了。当然,我们希望能够做到APP已打开的话便直接跳转到当前已经打开的页面,没有打开则开启APP。具体实现请见第三方唤醒APP以及四种启动模式的思考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 短链接打开应用的方式有很多种,下面介绍其一种常见的方法。 在 Android ,我们可以通过使用自定义 URL Scheme 或者使用深度链接的方式打开应用。自定义 URL Scheme 是一种特殊的 URL,用于唤起应用程序并执行特定操作。 首先,我们需要在 AndroidManifest.xml 文件注册自定义 URL Scheme。我们可以在 `<data>` 元素指定一个自定义的 host 或者 path。 ```xml <activity android:name=".MainActivity" android:label="@string/app_name"> <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="你的应用域名" android:scheme="你的自定义 URL Scheme"/> </intent-filter> </activity> ``` 然后,在你的应用使用 Intent 进行处理。当用户点击短链接时,系统会自动将链接传递给应用程序的 MainActivity,你可以在 onCreate 方法获取到链接并执行相应的操作。 ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = getIntent(); Uri data = intent.getData(); if (data != null) { // 在此处根据链接执行特定操作 } } ``` 要创建一个短链接并将其指向你的应用程序,你可以使用一些第三方服务,例如 Firebase Dynamic Links 等。 总结起来,通过自定义 URL Scheme 或者使用深度链接的方式,我们可以实现在 Android 通过短链接来打开应用程序,并根据链接执行特定操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值