import androidx.appcompat.app.AppCompatActivity; import androidx.browser.customtabs.CustomTabsClient; import androidx.browser.customtabs.CustomTabsIntent; import androidx.browser.customtabs.CustomTabsServiceConnection; import androidx.browser.customtabs.CustomTabsSession; import android.annotation.SuppressLint; import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.webkit.WebResourceError; import android.webkit.WebResourceRequest; import android.webkit.WebResourceResponse; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import java.io.File; /* * vite * */ public class MainActivity extends AppCompatActivity { @SuppressLint({"MissingInflatedId", "SetJavaScriptEnabled"}) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化WebView WebView webView = findViewById(R.id.web_view); // 设置WebViewClient来处理页面加载 webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { // 如果不是app内处理的链接,则使用Custom Tabs打开 if (!request.getUrl().getHost().contains("yourdomain.com")) { // openInCustomTabs(request.getUrl().toString()); return true; // 表示该链接已经被处理,不需要WebView加载 } return super.shouldOverrideUrlLoading(view, request); } @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Log.e("WebView Error", "Error code: " + error.getErrorCode() + ", Description: " + error.getDescription()); } @Override public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { super.onReceivedHttpError(view, request, errorResponse); Log.e("WebView HTTP Error", "Error code: " + errorResponse.getStatusCode() + ", Reason: " + errorResponse.getReasonPhrase()); } }); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); settings.setDomStorageEnabled(true); settings.setCacheMode(WebSettings.LOAD_DEFAULT); // 或 LOAD_CACHE_ELSE_NETWORK 等,根据需要选择 settings.setAppCacheEnabled(true); settings.setDatabaseEnabled(true); settings.setSupportZoom(true); settings.setBuiltInZoomControls(true); settings.setDisplayZoomControls(false); settings.setAllowContentAccess(true); // 允许内容访问 settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); // 允许混合内容 // 加载初始URL File crashReportsDir = new File(getCacheDir(), "WebView/Crash Reports"); if (!crashReportsDir.exists() && !crashReportsDir.mkdirs()) { Log.e("WebView Error", "Failed to create Crash Reports directory"); } try{ webView.loadUrl("https://vite服务器地址"); }catch (Exception e){ e.printStackTrace(); } } // 使用Custom Tabs打开链接 private void openInCustomTabs(String url) { CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent customTabsIntent = builder.build(); customTabsIntent.launchUrl(this, Uri.parse(url)); } }
标签:settings,request,import,Webview,true,WebView,android From: https://www.cnblogs.com/xl-7951/p/18519612