如何从内置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是否正确
Kotlin回答:
val browserIntent = Intent(Intent.ACTION_VIEW, uri)
ContextCompat.startActivity(context, browserIntent, null)
我在Uri上添加了一个扩展,以使这更加容易
myUri.openInBrowser(context)
fun Uri?.openInBrowser(context: Context) {
this ?: return // Do nothing if uri is null
val browserIntent = Intent(Intent.ACTION_VIEW, this)
ContextCompat.startActivity(context, browserIntent, null)
}
另外,这里有一个简单的扩展函数,可以将字符串安全地转换为Uri。
"https://stackoverflow.com".asUri()?.openInBrowser(context)
fun String?.asUri(): Uri? {
return try {
Uri.parse(this)
} catch (e: Exception) {
null
}
}