應用需導引使用者至外部網頁時,最簡單方式是發送 ACTION_VIEW Intent 並攜帶網址 URI。系統會自動啟動預設瀏覽器或使用者選擇的應用。本文示範最精簡寫法與進階控制。
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));
startActivity(browserIntent);
IntentFilter 解析可處理 VIEW + http/https scheme 的 Activity。Intent.createChooser(browserIntent, "Open with")。| 需求 | 做法 |
|---|---|
| 在 App 內嵌瀏覽 | 使用 Chrome Custom Tab 或 WebView |
| 攜帶 Referrer Header | intent.putExtra(Intent.EXTRA_REFERRER, Uri.parse("android-app://your.package")) |
| 處理 Deep Link | 在 Manifest 宣告 <intent-filter> 捕獲特定 scheme/host |
createChooser()。一行程式碼即可串聯系統瀏覽器,是 Android 應用整合外部網頁最輕量的方式。