前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android 如何使用短信链接打开APP

Android 如何使用短信链接打开APP

作者头像
砸漏
发布2020-11-05 13:01:50
4K0
发布2020-11-05 13:01:50
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

短信链接跳转APP

平时我们会收到广告短信,比如某东,某宝,里面附加着链接,当你点开链接(手机自带的浏览器),发现浏览器打开后,等一下下,就会打开对应的APP,直接到广告相应的页面。

Android端的代码

从简单的开始,第一个启动的Activity先来处理

代码语言:javascript
复制
<activity android:name=".activity.ActivityFirst" 
  <intent-filter 
    <action android:name="android.intent.action.MAIN" / 
 
    <category android:name="android.intent.category.LAUNCHER" / 
  </intent-filter 
  <!-- 不可以把上面,桌面启动图标的intent-filter,跟下面短信打开App的intent-filter写一起,否者没有桌面图标-- 
  <!-- 在启动的activity加入以下代码,其中scheme很重要,短信启动App的标识吧 -- 
  <intent-filter 
    <data android:scheme="baozi" / 
 
    <action android:name="android.intent.action.VIEW" / 
    <category android:name="android.intent.category.DEFAULT" / 
    <category android:name="android.intent.category.BROWSABLE" / 
  </intent-filter 
</activity 

2.HTML代码,关键就是href,就是之前Android启动intent-filter的 “ [scheme的内容]” + “ :” 这个冒号不能少

代码语言:javascript
复制
<!DOCTYPE html 
<html 
<head 
  <title Android短信测试</title 
</head 
<body 
  <a href="baozi:" rel="external nofollow"  启动程序</a 
</body 
</html 

3.测试一下,能不能启动App,我们没有服务器的情况下,可以把这段HTML代码拷贝到手机里,点击选择品牌自带浏览器启动就可以啦。

基本启动功能.gif

最基本的功能实现啦,然后我再传递参数,打开指定的页面。

1.HTML的跳转链接里面添加参数

代码语言:javascript
复制
<a href=" scheme的内容 :// host的内容?传递参数的key=传递参数的value" rel="external nofollow"  随意什么内容...</a 

<a href="baozi://bao.cn?type=red&url=111&name=红色" rel="external nofollow" rel="external nofollow"  启动红色程序</a 
<a href="baozi://bao.cn?type=yellow&name=黄色" rel="external nofollow" rel="external nofollow"  启动黄色色程序,url空</a 
<a href="baozi://bao.cn?type=green&url=333" rel="external nofollow" rel="external nofollow"  启动绿色程序,name空</a 

scheme:启动的App的标识,相当于协议吧。

host:域名,不重要。

query:传给app参数的Key和Value 。

2.Android代码,在第一启动页加入下面代码

代码语言:javascript
复制
public static final String TYPE_INTENT = "type";
  public static final String URL_INTENT = "url";
  public static final String NAME_INTENT = "name";

  if (intent.getData() != null)
    {
      Uri uri = intent.getData();
      uri.getScheme();//获取scheme
      uri.getHost();//获取host
      uri.getAuthority();//获取authority
      String type = uri.getQueryParameter(TYPE_INTENT);//获取传递参数
      String url = uri.getQueryParameter(URL_INTENT);
      String name = uri.getQueryParameter(NAME_INTENT);
      //标题转UTF-8码
      if (!TextUtils.isEmpty(name))
      {
        try
        {
          name = URLDecoder.decode(name, "UTF-8");
        } catch (UnsupportedEncodingException e)
        {
          e.printStackTrace();
        }
      }
    }

参数可以传空的,如果是中文要转码,断点看看参数

3.测试。

参数跳转.gif

4.总结,短信跳转App难度不大,就是基本用原生或者chrome内核的浏览器,支持跳转,其他浏览器兼容问题会有。

5.代码不多,就直接放出来。

ActivityFirst代码

代码语言:javascript
复制
public class ActivityFirst extends AppCompatActivity
{
  public static final String TYPE_INTENT = "type";
  public static final String URL_INTENT = "url";
  public static final String NAME_INTENT = "name";
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_activity);

    //如果是从网址打开的
    Intent intent = getIntent();
    if (intent.getData() != null)
    {
      Uri uri = intent.getData();
      uri.getScheme();//获取scheme
      uri.getHost();//获取host
      uri.getAuthority();//获取authority
      String type = uri.getQueryParameter(TYPE_INTENT);
      String url = uri.getQueryParameter(URL_INTENT);
      String name = uri.getQueryParameter(NAME_INTENT);
      //标题转UTF-8码
      if (!TextUtils.isEmpty(name))
      {
        try
        {
          name = URLDecoder.decode(name, "UTF-8");
        } catch (UnsupportedEncodingException e)
        {
          e.printStackTrace();
        }
      }

      //获取到的参数跳转
      Intent intentStart = new Intent(this,ActivityMain.class);
      intentStart.putExtra(TYPE_INTENT,type);
      intentStart.putExtra(URL_INTENT,url);
      intentStart.putExtra(NAME_INTENT,name);
      startActivity(intentStart);
      finish();
    }
  }
}

HTML代码

代码语言:javascript
复制
<!DOCTYPE html 
<html 
<head 
  <title Android短信测试</title 
</head 
<body 
  <a href="baozi://bao.cn?type=red&url=111&name=红色" rel="external nofollow" rel="external nofollow"  启动红色程序</a 
  <br 
  <a href="baozi://bao.cn?type=yellow&name=黄色" rel="external nofollow" rel="external nofollow"  启动黄色色程序,url空</a 
  <br 
  <a href="baozi://bao.cn?type=green&url=333" rel="external nofollow" rel="external nofollow"  启动绿色程序,name空</a 

</body 
</html 

Manifest代码最上面有了

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档