一、URL Scheme 概述
1、什麼是 URL Scheme
在移動開發中,URL Scheme 是一種特殊的鏈接,可以用來喚起 APP 並執行對應的操作。URL Scheme 通過協議頭(如:http、https、ftp 等)來識別鏈接的類型,通過 host 和 path 參數來識別鏈接的子類型。在 Android 開發中,我們使用 intent-filters 來註冊 URL Scheme,以從系統中接收對應的 Intent。
2、URL Scheme 的優點
· 方便使用:用戶可以通過瀏覽器、郵件、短信等多種方式觸發 URL Scheme,並打開相應的 APP。
· 常用功能:URL Scheme 可以實現應用內跳轉、分享內容、執行系統操作等常用功能。
· 多個 APP 協同:URL Scheme 可以實現多個 APP 間的協同,比如調用支付寶 APP 完成付款等。
二、URL Scheme 的註冊和調用
1、URL Scheme 的註冊
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="myscheme" android:host="mypage" /> </intent-filter> </activity>
· 在 AndroidManifest.xml 文件中,我們需要為對應的 Activity 註冊一個 intent-filter。
· action 參數固定為 android.intent.action.VIEW。
· category 參數固定為 android.intent.category.DEFAULT。
· data 參數用來定義 URL Scheme 的協議頭和 host 名稱。
2、URL Scheme 的調用
Uri uri = Uri.parse("myscheme://mypage?param1=value1¶m2=value2"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
· 我們可以通過 Uri.parse(String uriString) 方法將字符串轉換為 Uri 對象。
· 我們通過 Intent(Intent.ACTION_VIEW, uri) 構造方法創建一個 Intent,用於跳轉到對應的 Activity。
· 我們使用 startActivity(intent) 方法執行跳轉。
三、URL Scheme 的參數傳遞
1、獲取 URL Scheme 中的參數
Intent intent = getIntent(); if (intent != null) { Uri uri = intent.getData(); if (uri != null) { String param1 = uri.getQueryParameter("param1"); String param2 = uri.getQueryParameter("param2"); } }
· 我們使用 getIntent() 方法獲取當前 Activity 的 Intent 對象。
· 我們使用 uri.getQueryParameter(String key) 方法獲取 URL Scheme 的參數。
2、在 URL Scheme 中傳遞數據和文件
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="myscheme" android:host="mypage" /> <data android:scheme="file" /> <data android:scheme="content" /> <data android:mimeType="*/*" /> </intent-filter> </activity>
· 我們可以通過添加多個 data 標籤,來實現 URL Scheme 和數據文件的傳遞。
· 在 AndroidManifest.xml 中,我們可以通過 mimeType 參數來指定支持的文件類型。
四、URL Scheme 的跳轉進階
1、暗示式 Intent 傳遞參數
Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); Uri uri = Uri.parse("myscheme://mypage"); intent.setData(uri); intent.putExtra("param1", "value1"); intent.putExtra("param2", "value2"); startActivity(intent);
· 使用 setData(Uri data) 方法來設置 Intent 中的 Uri 參數。
· 使用 putExtra(String key, Bundle value) 方法來傳遞額外的 Bundle 類型數據。
2、使用 FLAG_ACTIVITY_NEW_TASK 參數啟動 Activity
Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); Uri uri = Uri.parse("myscheme://mypage"); intent.setData(uri); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
· 使用 setFlags(int flags) 方法並添加 Intent.FLAG_ACTIVITY_NEW_TASK 標誌,可以實現在新任務棧中啟動 Activity。
五、錯誤處理和安全機制
1、判斷 URL Scheme 是否可用
Intent intent = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("myscheme://mypage?param1=value1¶m2=value2"); intent.setData(uri); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(MainActivity.this, "未安裝對應應用", Toast.LENGTH_SHORT).show(); }
· 可以通過調用 Intent.resolveActivity(PackageManager pm) 方法來判斷 URL Scheme 是否可用。
· 如果返回 null,則代表沒有對應的 APP 能夠處理該 Url。
2、Prevent XSS 攻擊
XSS(跨站腳本)是一種常見的 Web 攻擊。為了避免 XSS 攻擊,我們需要對從 URL Scheme 中獲取的參數進行安全處理,比如使用 UrlEncode 分隔符編碼,或不接受外部不可信數據。
六、總結
通過本文的講解,我們了解了 Android URL Scheme 的概念、註冊和調用方法,以及參數傳遞、跳轉進階、錯誤處理和安全機制等相關知識。對於移動開發工程師來說,掌握 URL Scheme 的使用技巧至關重要,可以幫助我們更好地實現 APP 功能,提升用戶體驗。
原創文章,作者:ROJTI,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/351759.html