Android中H5通过scheme吊起app内的相关界面

原生的App分享出去的界面都是H5界面,如果想在H5界面打开app的某一界面,这时候就需要用到Scheme协议。

什么是 URL Scheme

概述:
android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定义自己的scheme协议,可以非常方便跳转app中的各个页面;通过scheme协议,服务器可以定制化告诉App跳转那个页面,可以通过通知栏消息定制化跳转页面,可以通过H5页面跳转页面等。

URL Scheme协议格式:

URL Scheme 属性分为,Scheme,Host,port,path,query,

test://shangjia/shangjiaDetail?shagnjiaId=222

通过上面的路径我们来分析,

  • scheme : test
  • host : shangjia
  • path : shangjiaDetail
  • query : shangjiaId=222

实现步骤一:

配置你要分享的Activity

<intent-filter >
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
                />

                <data
                    android:host="com.test.cn"
                    android:path="/test1"
                    android:port="8888"
                    android:scheme="test" />
            </intent-filter >

步骤二:

在App里写一个测试,代码如下

//测试sscheme的跳转的
//        findViewById(R.id.button).setOnClickListener(view -> {
//
//            String url = "test://com.test.cn:8888/test1?id=10011002;                     
//
//            Intent intent = new Intent(Intent.ACTION_VIEW,
//                    Uri.parse(url));
//            context.startActivity(intent);
//
//        });

步骤三:

在要吊起的类中获取相关参数:

 public void setScheme() {
        Intent intent = getIntent();
        Uri uri = intent.getData();
        if (null == uri) {

            return;
        }
        //获得scheme里的相关参数
        String id =uri.getQueryParameter("id");
}

注意一点 ,有的手机浏览器可以打开app却无法唤起指定界面,这个时候可以换个浏览器再试一下,简单来说使用Scheme还是很easy的。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章