アプリケーション内ではなく、組み込みの 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
ベストアンサー1
これを試して:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
それは私にとってはうまくいきます。
欠落している「http://」については、次のようにします。
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
また、ユーザーが「http://」で URL を入力している EditText を事前に入力しておくことも考えられます。
Kotlin の場合:
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://$url"
}
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(browserIntent)