android-Scheme與網頁跳轉原生的三種方式
什麼是 URL Scheme?
android中的scheme是一種頁面內跳轉協議,是一種非常好的實現機制,通過定義自己的scheme協議,可以非常方便跳轉app中的各個頁面;通過scheme協議,伺服器可以定製化告訴App跳轉那個頁面,可以通過通知欄訊息定製化跳轉頁面,可以通過H5頁面跳轉頁面等。
URL Scheme應用場景:
客戶端應用可以向作業系統註冊一個 URL scheme,該 scheme 用於從瀏覽器或其他應用中啟動本應用。通過指定的 URL 欄位,可以讓應用在被調起後直接開啟某些特定頁面,比如商品詳情頁、活動詳情頁等等。也可以執行某些指定動作,如完成支付等。也可以在應用內通過 html 頁來直接呼叫顯示 app 內的某個頁面。綜上URL Scheme使用場景大致分以下幾種:
伺服器下發跳轉路徑,客戶端根據伺服器下發跳轉路徑跳轉相應的頁面
H5頁面點選錨點,根據錨點具體跳轉路徑APP端跳轉具體的頁面
APP端收到伺服器端下發的PUSH通知欄訊息,根據訊息的點選跳轉路徑跳轉相關頁面
APP根據URL跳轉到另外一個APP指定頁面
URL Scheme協議格式:
String urlStr="http://www.orangecpp.com:80/tucao?id=hello&name=lily";
//url = protocol + authority(host + port) + path + query
//協議protocol= http
//域名authority= www.orangecpp.com:80
//頁面path= /tucao
//引數query= id=hello&name=lily
//authority = host + port
//主機host= www.orangecpp.com
//埠port= 80
URL Scheme如何使用:
1.)設定Scheme
在AndroidManifest.xml中對標籤增加設定Scheme
<activity
android:name=".GoodsDetailActivity"
android:theme="@style/AppTheme">
<!--要想在別的App上能成功調起App,必須新增intent過濾器-->
<intent-filter>
<!--協議部分,隨便設定
xl://goods:8888/goodsDetail?goodsId=10011002
-->
<data android:scheme="xl"
android:host="goods"
android:path="/goodsDetail"
android:port="8888"/>
<!--下面這幾行也必須得設定-->
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
2.)獲取Scheme跳轉的引數
Uri uri = getIntent().getData();
if (uri != null) {
// 完整的url資訊
String url = uri.toString();
Log.e(TAG, "url: " + uri);
// scheme部分
String scheme = uri.getScheme();
Log.e(TAG, "scheme: " + scheme);
// host部分
String host = uri.getHost();
Log.e(TAG, "host: " + host);
//port部分
int port = uri.getPort();
Log.e(TAG, "host: " + port);
// 訪問路勁
String path = uri.getPath();
Log.e(TAG, "path: " + path);
List<String> pathSegments = uri.getPathSegments();
// Query部分
String query = uri.getQuery();
Log.e(TAG, "query: " + query);
//獲取指定引數值
String goodsId = uri.getQueryParameter("goodsId");
Log.e(TAG, "goodsId: " + goodsId);
}
3.)呼叫方式
網頁上
<a href="xl://goods:8888/goodsDetail?goodsId=10011002">開啟商品詳情</a>
原生呼叫
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
startActivity(intent);
4.)如何判斷一個Scheme是否有效
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
startActivity(intent);
}
webview跳轉到activity 示例:
參考:
本地網頁(放在assets目錄下):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div>
<a href="TOPICID://aa.bb:80/test?p=12&d=1">重定向和scheme跳轉button</a>
</div>
<div>
<input type="button" value="js與android互動button" onClick="javascript:android.toActivity()"/>
</div>
</body>
</html>
方式一:shouldOverrideUrlLoading重定向
<WebView
android:id="@+id/myWebView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
/*shouldOverrideUrlLoading重定向 跳轉*/
WebView myWebView1 = (WebView) findViewById(R.id.myWebView1);
myWebView1.loadUrl("file:///android_asset/test1.html");//載入本地的url
myWebView1.setWebViewClient(new myWebViewClient());
private class myWebViewClient extends WebViewClient {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/*此處獲取的url的scheme都是小寫*/
if (!TextUtils.isEmpty(url) && url.contains("topicid://aa.bb:80/test?p=12&d=1")) {
Intent intent = new Intent();
intent.setClass(WebViewActivity.this, MainActivity.class);
startActivity(intent);
}
return true;
}
}
在Android通過WebViewClient複寫shouldOverrideUrlLoading ():
優點:不存在通過 WebView的addJavascriptInterface()進行物件對映 的漏洞;
缺點:JS獲取Android方法的返回值複雜。
如果JS想要得到Android方法的返回值,只能通過 WebView 的 loadUrl ()去執行 JS 方法把返回值傳遞回去
方式二:scheme 跳轉
<WebView
android:id="@+id/myWebView2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1" />
/*scheme 跳轉*/
//scheme跳轉,不可重寫shouldOverrideUrlLoading方法,否則scheme不起作用
WebView myWebView2 = (WebView) findViewById(R.id.myWebView2);
myWebView2.loadUrl("file:///android_asset/test1.html");
需要配置intent-filter,不可以重寫shouldOverrideUrlLoading
<!--跳轉後的頁面-->
<activity
android:name="com.example.webviewtest.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!--TOPICID://aa.bb:80/test?p=12&d=1-->
<!--scheme類似http和https,注意要轉換為小寫-->
<data
android:host="aa.bb"
android:path="/test"
android:port="80"
android:scheme="topicid" />
</intent-filter>
</activity>
方式三:js與android互動 跳轉
<WebView
android:id="@+id/myWebView3"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1" />
/*js與android互動 跳轉*/
WebView myWebView3 = (WebView) findViewById(R.id.myWebView3);
myWebView3.loadUrl("file:///android_asset/test1.html");
myWebView3.getSettings().setJavaScriptEnabled(true);
// 與js互動,JsCallAndroidinterface 是個介面,與js互動時用到的,
//這個介面實現了從網頁跳到app中的activity 的方法,特別重要
myWebView3.addJavascriptInterface(new JsCallAndroidinterface(this), "android");
private class JsCallAndroidinterface {
Activity mActivity;
JsCallAndroidinterface(Activity mActivity) {
this.mActivity = mActivity;
}
/**
* 與js互動時用到的方法,在js裡直接呼叫的
* 定義JS需要呼叫的方法,被JS呼叫的方法必須加入@JavascriptInterface註解,
* 否則HTML中的js報錯:toActivity不是一個function
*/
@JavascriptInterface
public void toActivity() {
System.out.println("----------------toActivity--");
mActivity.startActivity(new Intent(mActivity, MainActivity.class));
}
}
通過 WebView的addJavascriptInterface()進行物件對映:
優點:使用簡單 , 僅將Android物件和JS物件對映即可
缺點:存在嚴重的漏洞問題,具體請看文章:
你不知道的 Android WebView 使用漏洞