概述:
android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定义自己的scheme协议,可以非常方便跳转app中的各个页面;通过scheme协议,服务器可以定制化告诉App跳转那个页面,可以通过通知栏消息定制化跳转页面,可以通过H5页面跳转页面等。
概述:
客户端应用可以向操作系统注册一个URL Scheme,该scheme用于从浏览器或其他应用中启动本应用,通过scheme协议来跳转到相应的APP界面,比如商品详情,活动详情,商家详情等等界面。也可以执行某些指定动作,如完成支付等。也可以在应用内部通过H5页面来直接跳转APP某个界面。
URL Scheme应用场景分为以下4中:
URL Scheme 属性分为,Scheme,Host,port,path,query,
test://shangjia/shangjiaDetail?shagnjiaId=222
通过上面的路径我们来分析,
我们可以用代码来验证:
验证的地址是这个:test://start/?id=431&name=zhouyuan&age=23
Intent intent = getIntent(); String action = intent.getAction(); String scheme = intent.getScheme(); Uri uri = intent.getData(); System.out.println("action:" + action); System.out.println("scheme:" + scheme); if (uri != null) { String host = uri.getHost(); String dataString = intent.getDataString(); String id = uri.getQueryParameter("id"); String name = String.valueOf(uri.getQueryParameters("name")); String age = uri.getQueryParameter("age"); String path = uri.getPath(); String path1 = uri.getEncodedPath(); String queryString = uri.getQuery(); System.out.println("host:" + host); System.out.println("dataString:" + dataString); System.out.println("id:" + id); System.out.println("name:" + name); System.out.println("age:" + age); System.out.println("path:" + path); System.out.println("path1:" + path1); System.out.println("queryString:" + queryString); }
看图
从这个例子我们能看出:我们是想跳入商家详情界面。主要的这些参数,都是需要自己去和前端去达成一个协议。并不是说非要按照我的这个例子来。比如你的项目是做汽车的,可以把URL写成
test://qiche/qicheDetail?qicheId=222
这些都没什么影响。
<application android:name=".MyApplication" android:allowBackup="true" android:icon="@drawable/logo" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".activity.StartActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <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:host="web" android:scheme="test" /> <data android:host="start" android:scheme="test" /> </intent-filter> </activity> <activity android:name=".activity.LoginActivity"> <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="login" android:scheme="test" /> </intent-filter> </activity>
当网页或者通知栏,Android代码发送这种规则scheme时这时候就会调起相对应界面。
那么接下来我们就该测试一波:
button.setOnClickListener(v -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("test://login/123123123"))));
看下图:
这个前提,你需要在AndroidMainfest.xml配置,你看上面的代码。
2.通过H5页面的锚点跳转相应的页面
html代码:
<html><head><title>Js调用Android</title></head><body><a href="test://start/?id=431&name=zhouyuan&age=23">跳转start</a><a href="test://web/?id=431&name=zhouyuan&age=23">跳转web</a><a href="test://login/?id=431&name=zhouyuan&age=23">跳转Login</a></body></html>//webview代码webview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { System.out.println("view = [" + view + "], url = [" + url + "]"); return true; } });
这个具体的我就不再讲了,我们已经获取到了URL Scheme协议地址。我们可以直接用一些正则来取到相应的字符串,然后根据该字符串来判断,跳到相应的界面。
如果你觉得改方法比较麻烦。那可以直接在AndroidMainfest.xml配置相应的属性,前提是URL协议地址要相应。不然你是跳转不了的。
具体的可以自行测试,就是把 webview.setWebViewClient(new WebViewClient()){}给注释掉。
我们点击跳转Login时,会有意想不到的效果。具体还是不建议这么做,自己想一想,前面也讲到在项目中可能URL Scheme协议并不止一个界面。如果你在AndroidMainfest.xml里面去给每一个可能相关的界面都配置scheme属性,那你整个界面看着也不美观,而且还都是重复的配置。所以还是建议根据URL地址来判断跳转。
3.根据服务器下发通知栏消息,APP跳转相应的界面
其实,我们简单的想一下,都是同样的逻辑。把服务器下发的通知栏消息,里面的URL地址数据拿到,进行解析判断,然后跳转到相应的界面。具体代码就不贴了。
总结:
Android中的URL Scheme是一个非常好的机制,我们可以通过自定义Scheme属性,让我们方便,灵活的在APP内随意跳转。如果不明白请留言。
联系客服