How to embed HTML code in Android app?

依然范特西╮ 提交于 2020-01-22 15:12:25

问题


I am writing an app for which I need to embed HTML object in the Android app, I followed the tutorial given here: http://mobile.tutsplus.com/tutorials/android/android-sdk-embed-a-webview-with-the-webkit-engine/ and modified the code accordingly, following is the code, but I am getting a white screen...

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView engine = (WebView) findViewById(R.id.web_engine);

    engine.getSettings().setJavaScriptEnabled(true);
    engine.getSettings().setPluginsEnabled(true); 
    engine.getSettings().setAppCacheEnabled(false); 
    engine.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 

        String data = "<html>" +
                "<body>" +
                "bb"+
                "<object width=\"450\" height=\"300\" align=\"middle\"><param name=\"movie\" value=\"http://www.raaga.com/widgets/top10/widgettopten.swf?l=H&q=1\" /><param name=\"wmode\" value=\"transparent\"><embed src=\"http://www.raaga.com/widgets/top10/widgettopten.swf?l=H&q=1\" width=\"450\" height=\"300\" align=\"middle\" type=\"application/x-shockwave-flash\" wmode=\"transparent\"  FlashVars=\"gig_lt=1312323434023&gig_pt=1312325057958&gig_g=2\"/> <param name=\"FlashVars\" value=\"gig_lt=1312323434023&gig_pt=1312325057958&gig_g=2\" /></object>"+
                "gg"+
                "</body>" +
                "</html>";

        engine.loadData(data, "text/html", "UTF-8");
    }
}

The object that I want to embed is:

<object width="450" height="300" align="middle"><param name="movie" value="http://www.raaga.com/widgets/top10/widgettopten.swf?l=H&q=1" /><param name="wmode" value="transparent"><embed src="http://www.raaga.com/widgets/top10/widgettopten.swf?l=H&q=1" width="450" height="300" align="middle" type="application/x-shockwave-flash" wmode="transparent"  FlashVars="gig_lt=1312323434023&gig_pt=1312326303531&gig_g=2"/> <param name="FlashVars" value="gig_lt=1312323434023&gig_pt=1312326303531&gig_g=2" /></object>

回答1:


Put the file in the /assets directory, you can access it with the file:///android_asset/ directory.

Instead of putting the html in code the assets folder is where you can put static data files such as HTML and its related CSS, images, and JavaScript files, etc. Then you can use WebView.loadUrl(). This has the advantage of not having to be in code with Java String escapes and cumbersome editing. You will be able to create the file(s) separately, in your favorite web project editor, then copy and paste them. If you need to edit the file at runtime you can still load the file from the assets directory and still not have to worry about Java String escapes and cumbersome editing. Then apply the edits and pass to WebView.loadData().

To help debug this problem implement WebChromeClient.onLoadResource and check out the console message tutorial.

Don't forget to use PackageManager.getPackageInfo() to check if flash is available. If its not you can give the user the option to download it from the market.




回答2:


Android has no official Flash viewer, so it doesn't know what to do with an embedded SWF.

If you put other (visible) HTML in your code, you should be able to see it in the view.

If you try to access your SWF through the webbrowser, you should get a broken/missing plugin icon.




回答3:


Did you declare the Internet permission in your Android manifest? In order to access the SWF which is on a remote server you will need that.




回答4:


You need to enable plugins in your webview:

engine.getSettings().setPluginsEnabled(true);



回答5:


Embedding HTML files is very simple, and loading it into WebView is even simpler. Here:

Create your HTML file, and put it in your project's assets file. in my case: "proj/assets/license.html"

    WebView w = new WebView(this);
    // Load the asset file by URL. Note the 3 slashes "file:///".
    w.loadUrl("file:///android_asset/license.html");

That's it.



来源:https://stackoverflow.com/questions/6919800/how-to-embed-html-code-in-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!