原生的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的。