在ANDROID BROWSER中加载HTML代码

时间:2012-03-10 08:16:40

标签: 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);

如何在浏览器中加载HTML源代码?请以一个例子来解释。

1 个答案:

答案 0 :(得分:4)

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);