在 ANDROID 浏览器中加载 HTML 代码

RBR*_*RBR 2 android android-intent

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

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

djp*_*ado 5

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);
Run Code Online (Sandbox Code Playgroud)