萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 手機 >  >> 手機應用 >> Android 教學

Android 開發:Intent 調用系統瀏覽器打開網頁完整代碼示例

在 Android 應用中調用系統瀏覽器打開指定 URL,核心代碼僅需 5 行,利用 Intent.ACTION_VIEW 隱式意圖實現。

核心代碼

// 按鈕點擊監聽
link.setOnClickListener(buttonListener);

// 響應方法
case R.id.imageButton1:
    link();
    break;

// 打開網頁方法
private void link() {
    String url = "https://www.webadress.cn"; // 目標網址
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    startActivity(intent);
}

必要導入

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;

效果演示

點擊按鈕後,系統彈出瀏覽器選擇器(或直接跳轉默認瀏覽器)加載目標頁面。可將 URL 替換為任意合法鏈接(http/https/tel/mailto 等)。

擴展: 若需指定瀏覽器包名,可用 intent.setPackage("com.android.chrome");若需在 WebView 內打開,則另行實現。

copyright © 萬盛學電腦網 all rights reserved