轉(zhuǎn)載自 https://blog.csdn.net/m0_37700275/article/details/81386910
一、URL Scheme使用場(chǎng)景介紹
URL Scheme使用場(chǎng)景双吆,目前1眨唬,2会前,5使用場(chǎng)景很廣,有沒(méi)有一種熟悉的感覺(jué)匾竿?
- 通過(guò)小程序瓦宜,利用Scheme協(xié)議打開(kāi)原生app。
- H5頁(yè)面點(diǎn)擊錨點(diǎn)岭妖,根據(jù)錨點(diǎn)具體跳轉(zhuǎn)路徑APP端跳轉(zhuǎn)具體的頁(yè)面临庇。
- APP端收到服務(wù)器端下發(fā)的PUSH通知欄消息,根據(jù)消息的點(diǎn)擊跳轉(zhuǎn)路徑跳轉(zhuǎn)相關(guān)頁(yè)面昵慌。
- APP根據(jù)URL跳轉(zhuǎn)到另外一個(gè)APP指定頁(yè)面假夺。
- 通過(guò)短信息中的url打開(kāi)原生app。
二斋攀、URL Scheme基礎(chǔ)介紹
什么是URL Scheme已卷?
- android中的scheme是一種頁(yè)面內(nèi)跳轉(zhuǎn)協(xié)議,是一種非常好的實(shí)現(xiàn)機(jī)制淳蔼,通過(guò)定義自己的scheme協(xié)議争舞,可以非常方便跳轉(zhuǎn)app中的各個(gè)頁(yè)面困后。
URL Scheme協(xié)議格式
String urlStr="http://www.ycbjie.cn:80/yc?id=hello&name=cg";
//url = protocol + authority(host + port) + path + query
//協(xié)議protocol= http
//域名authority= www.ycbjie.cn:80
//頁(yè)面path= /yc
//參數(shù)query= id=hello&name=cg
//authority = host + port
//主機(jī)host= www.ycbjie.cn
//端口port= 80
Scheme鏈接格式樣式
- 樣式:[scheme]??/[host]/[path]?[query]
三啤咽、URL Scheme如何使用
設(shè)置Scheme
在AndroidManifest.xml中對(duì)標(biāo)簽增加設(shè)置Scheme
<activity
android:name=".ui.main.ui.activity.SchemeFirstActivity"
android:screenOrientation="portrait">
<!--Android 接收外部跳轉(zhuǎn)過(guò)濾器-->
<!--要想在別的App上能成功調(diào)起App序攘,必須添加intent過(guò)濾器-->
<intent-filter>
<!-- 協(xié)議部分配置 ,注意需要跟web配置相同-->
<!--協(xié)議部分,隨便設(shè)置 yc://ycbjie:8888/from?type=yangchong -->
<data android:scheme="yc"
android:host="ycbjie"
android:path="/from"
android:port="8888"/>
<!--下面這幾行也必須得設(shè)置-->
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
獲取Scheme跳轉(zhuǎn)的參數(shù)存皂,并添加跳轉(zhuǎn)方式
public class SchemeFirstActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri uri = getIntent().getData();
if (uri != null) {
//獲取指定參數(shù)值
String type = uri.getQueryParameter("type");
Log.e( "UrlUtils","main: " + type);
if(type.equals("yangchong")){
ActivityUtils.startActivity(GuideActivity.class);
}else if(type.equals("main")){
ActivityUtils.startActivity(MainActivity.class);
}
}
finish();
}
}
調(diào)用方式
原生調(diào)用
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("yc://ycbjie:8888/from?type=yangchong"));
startActivity(intent);
網(wǎng)頁(yè)調(diào)用
<a href="yc://ycbjie:8888/from?type=yangchong">打開(kāi)叮咚app</a>
如何判斷一個(gè)Scheme是否有效
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("yc://ycbjie:8888/from?type=yangchong"));
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
startActivity(intent);
}
Scheme在短信息中注意要點(diǎn)
設(shè)置android:scheme="http"或者android:scheme="https"后晌坤,點(diǎn)擊短信息或者h(yuǎn)5頁(yè)面,發(fā)現(xiàn)沒(méi)有跳到指定的頁(yè)面旦袋,反而打開(kāi)的是網(wǎng)頁(yè)鏈接骤菠。