scheme协议

先说下DeepLink(深度链接)

DeepLink,又称深度链接、调起链接,是一套链接服务,用户点击链接可以跳转至 App 的特定深度页面。

DeepLink 常用调起方式主要有以下四种:

常用调起方式 适用平台 调起方法及特点
Universal Link iOS 9 及更高版本 通过 http 协议的链接直接打开 App,用户体验顺滑
AppLinks Android 6 及更高版本 通过 http 协议的链接直接打开 App,用户体验顺滑
scheme iOS 和 Android 所有版本 iOS 和 Android 通用的调起方式,打开前操作系统会询问是否打开目标 APP
剪切板 可操作设备剪切板的系统版本 通过剪切板的写入及读取,实现[延迟深度链接]功能

下面介绍下scheme方式

什么是 URL Scheme?

android中的scheme是一种页面内跳转协议。
通过定义自己的scheme协议,可以非常方便跳转app中的各个页面。
通过scheme协议,其他应用可以定制化跳转到我们APP内部页面。

Scheme协议在Android中使用场景

  • H5跳转到native页面

  • 客户端获取push消息中后,点击消息跳转到APP内部页面

  • APP根据URL跳转到另外一个APP指定页面

如何使用scheme协议

  • 在Mainefest配置文件中配置需要用scheme协议跳转的Activity
   <!-- scheme协议 -->
        <activity
            android:name=".SchemeActivity"
            android:label="@string/app_name">

            <!-- 要想在别的App上能成功调起App,必须添加intent过滤器 -->

                <!-- 协议部分,随便设置 -->
                <intent-filter>
                    <!--协议部分,随便设置-->
                    <data android:scheme="scheme" android:host="mtime" android:path="/goodsDetail" />
                    <!--下面这几行也必须得设置-->
                    <category android:name="android.intent.category.DEFAULT"/>
                    <action android:name="android.intent.action.VIEW"/>
                    <category android:name="android.intent.category.BROWSABLE"/>
                </intent-filter>
        </activity>
  • data部分都有哪些属性
<data android:host="string"
      android:mimeType="string"
      android:path="string"
      android:pathPattern="string"
      android:pathPrefix="string"
      android:port="string"
      android:scheme="string" />
  • 在java中使用通过协议跳转
/**
  * (1)在manifest配置文件中配置了scheme参数
  * (2)网络端获取url
  * (3)跳转
  * (4)由于系统的activity栈有bug,外部相同的scheme吊起详情页后,点返回首页;再从外部用          
  *一样的scheme吊起会无法跳转详情页。因为系统认为root activity是相同的,不给启动详情  
  *页,所以只能用这个方法,修改下intent data里的数据,加个时间戳进去
*/
String url = "scheme://mtime/goodsDetail?goodsId=10011002";
Uri.Builder openUri = Uri.parse(url).buildUpon();
openUri.appendQueryParameter("push_click_times",String.valueOf(System.currentTimeMillis()));
Intent intent = new Intent(Intent.ACTION_VIEW,openUri.build()); 
startActivity(intent);
  • H5跳转到native页面
<!DOCTYPE html>  
<html>  
<body>
<h1>Test Scheme</h1> 
<!--手动点击跳转-->
<a href="scheme://mtime/goodsDetail?goodsId=10011002">Click</a>
</body>  
</html>
  • 获取scheme协议参数
public class xxxActivity extends Activity {
    private static final String TAG = "xxxActivity";
    private TextView tvParam;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tvParam = (TextView) findViewById(R.id.tv_param);
        Uri data = getIntent().getData();
        Log.i(TAG, "host = " + data.getHost() + " path = " + data.getPath() + " query = " + data.getQuery());
        String param = data.getQueryParameter("goodsId");
        schemeTv.setText("获取的参数为:" + param);
    }
}

参考文章:https://www.jianshu.com/p/f9f9f0aa0f86

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。