如何从内置web浏览器而不是应用程序中的代码打开URL?
我试过了:
try {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(download_link));
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No application can handle this request."
+ " Please install a webbrowser", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
但我有个例外:
No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com
基本介绍:
https://正在“代码”中使用该代码,这样中间的任何人都无法读取它们。这样可以保护您的信息免受黑客攻击。
http://仅用于共享目的,不安全。
关于您的问题:XML设计:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sridhar.sharedpreferencesstackoverflow.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:background="#228b22"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<Button
android:id="@+id/normal_search"
android:text="secure Search"
android:onClick="secure"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/secure_search"
android:text="Normal Search"
android:onClick="normal"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="9"
android:id="@+id/button_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal">
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
活动设计:
public class MainActivity extends Activity {
//securely open the browser
public String Url_secure="https://www.stackoverflow.com";
//normal purpouse
public String Url_normal="https://www.stackoverflow.com";
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=(WebView)findViewById(R.id.webView1);
}
public void secure(View view){
webView.setWebViewClient(new SecureSearch());
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(Url_secure);
}
public void normal(View view){
webView.setWebViewClient(new NormalSearch());
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(Url_normal);
}
public class SecureSearch extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String Url_secure) {
view.loadUrl(Url_secure);
return true;
}
}
public class NormalSearch extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String Url_normal) {
view.loadUrl(Url_normal);
return true;
}
}
}
Android Manifest.Xml权限:
<uses-permission android:name="android.permission.INTERNET"/>
您在执行此操作时面临问题:
获取清单权限url之间的多余空间检查您的url是否正确
试试看:
Uri uri = Uri.parse("https://www.google.com");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
或者如果您希望在活动中打开web浏览器,请执行以下操作:
WebView webView = (WebView) findViewById(R.id.webView1);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webView.loadUrl(URL);
如果您想在浏览器中使用缩放控制,则可以使用:
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
Kotlin溶液
所有答案都是在该url的默认应用程序中打开该url。我想总是在浏览器中打开任何url。我需要一些kotlin的解决方案,并实现了下面的代码。
fun getPackageNameForUrl(context: Context, url: String): String? {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
val resolveInfo = context.packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
return resolveInfo?.activityInfo?.packageName
}
fun openInBrowser(context: Context, url: String) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
val packageName = getPackageNameForUrl(context, "http://")
packageName?.takeIf {
it == "android"
}?.let { intent.setPackage(defaultBrowserPackageName); }
startActivity(context, intent, null)
}