android 分享链接打开App


首先做成HTML的页面,页面内容格式如下:

<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a> 
这一句就可以了。

各个项目含义如下所示:

scheme:判别启动的App。 ※详细后述

host:适当记述

path:传值时必须的key     ※没有也可以

query:获取值的Key和Value  ※没有也可以

 作为测试好好写了一下,如下:

<a href="myapp://jp.app/openwith?name=zhangsan&age=26">启动应用程序</a>  

 接下来是Android端。

首先在AndroidManifest.xml的MAIN Activity下追加以下内容。(启动Activity时给予)

※必须添加项

<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:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter>
HTML记述的内容加入<data …/>。
其中必须的内容仅scheme,没有其他内容app也能启动。

※注意事项:intent-filter的内容【android.intent.action.MAIN】和 【android.intent.category.LAUNCHER】这2个,不能与这次追加的内容混合。
 所以,如果加入了同一个Activity,请按以下这样做,否则会导致应用图标在桌面消失等问题。

<intent-filter>  
    <action android:name="android.intent.action.MAIN"/>  
    <category android:name="android.intent.category.LAUNCHER" />  
</intent-filter>  
<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:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter> 
这样的话,没有问题。

 接下来在Activity中需要取值的地方添加以下代码,我是直接写在OnCreate函数里的:

Intent i_getvalue = getIntent();  
String action = i_getvalue.getAction();  
  
if(Intent.ACTION_VIEW.equals(action)){  
    Uri uri = i_getvalue.getData();  
    if(uri != null){  
        String name = uri.getQueryParameter("name");  
        String age= uri.getQueryParameter("age");  
    }  
}
这样就能获取到URL传递过来的值了。


尊重作者劳动成果:https://blog.csdn.net/DickyQie/article/details/88934910

  • 1
    点赞
  • 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、付费专栏及课程。

余额充值