Android 中Scheme協議的使用詳解

2021-09-27 12:34:35 字數 1982 閱讀 4819

一、首先什麼是scheme?

客戶端應用可以向系統註冊乙個 url scheme,該scheme用於從瀏覽器或其他應用中啟動本應用。通過指定的 url 字段,可以讓應用在被調起後直接開啟某些特定頁面。也可以執行某些指定動作。綜上scheme使用場景大致分以下幾種:

1.伺服器下發跳轉路徑,客戶端根據伺服器下發跳轉路徑跳轉相應的頁面

二、scheme協議格式

上面說到scheme是一種頁面內跳轉協議,那協議一定有固定的格式,在android中scheme屬於uri,下面就是scheme的協議結構:

[scheme:]scheme-specific-part[#fragment]

[scheme:][//authority][path][?query][#fragment]

string url="";

//url = protocol + authority(host + port) + path + query

//協議protocol= http

//網域名稱authority= www.orangecpp.com:80

//主機host= www.orangecpp.com

//埠port= 80

//頁面path= /tucao

//引數query= id=hello&name=lily

在android中,除了scheme、authority是必須要有的,其它的幾個path、query、fragment,它們每乙個可以選擇性的要或不要,但順序不能變

三、scheme的使用方式

scheme的使用分為兩個部分,在呼叫方傳入intent以及被呼叫方設定攔截並解析資料:

1.呼叫方使用scheme的方式,基本有兩種:

(1)uri uri = uri.parse(schemestr);

intent intent = new intent(intent.action_view, uri);

startactivity(intent);

(2)開啟商品詳情

2.androidmanifest中設定增加***(intent-filter),增加設定scheme,在schemeactivity中通過重寫onnewintent方法獲取scheme跳轉引數

(1)androidmanifest中設定增加***(intent-filter),設定scheme

(2)在schemeactivity中獲取引數

public class schemeactivity extends activity 

}

四、在使用過程中會遇到的情況

(1)判斷scheme是否有效

packagemanager packagemanager = getpackagemanager();

intent intent = new intent(intent.action_view,

uri.parse("xl://goods:8888/goodsdetail?goodsid=10011002"));

listactivities = packagemanager.queryintentactivities(intent, 0);

boolean isvalid = !activities.isempty();

if (isvalid) {

startactivity(intent);

(2)如下兩個activity 當通過scheme 跳轉介面時 ,系統會提示選擇開啟方式 因為沒有精確匹配要跳哪個介面

(3)如果不同的鏈結都要跳到乙個activity,activity的配置

android:path="/path"/>

android:path="/route"/>

Android 中Scheme協議的使用詳解

伺服器下發跳轉路徑,客戶端根據 伺服器下發跳轉路徑跳轉相應的頁面 zymobi 3g2win 9999 macthdetail?macthid 222 time 10001scheme 代表該schema 協議名稱 zymobi host 代表schema作用於哪個位址域 3g2win port 代...

Unity中的協程

1.建立協程 迭代器 ienumerator private ienumerator test 裡面可以使用 yield return new waitforsecond 1 等待1s後執行後面的 yield return new waitforseconds 0.3f 等待0.3秒,一段指定的時間...

Unity中協程方法使用

開啟協程 void start 返回值是ienumerator 返回引數時使用 yield return null 協程方法的呼叫是startcoroutine changecolor ienumerator changecolor 關閉協程 關閉協程的方法需要和開啟協程的方法對應 不能使用star...