gpt4 book ai didi

android - 在 ANDROID 浏览器中加载 HTML 代码

转载 作者:搜寻专家 更新时间:2023-11-01 08:10:59 27 4
gpt4 key购买 nike

String url = "<html><body>hello World</body></html>";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

如何在浏览器中加载 HTML 源代码?请举例说明。

最佳答案

Intent i = new Intent();

// MUST instantiate android browser, otherwise it won't work
i.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
i.setAction(Intent.ACTION_VIEW);

String html = "<html><body>hello World</body></html>";

// May work without url encoding, but I think is advisable
// URLEncoder.encode replace space with "+", must replace again with %20
String dataUri = "data:text/html," + URLEncoder.encode(html).replaceAll("\\+","%20");
i.setData(Uri.parse(dataUri));

startActivity(i);

关于android - 在 ANDROID 浏览器中加载 HTML 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9644974/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com