如何在Fragment中使用phonegap的CordovaWebView

来源:互联网 发布:知乎提问该问题已存在 编辑:程序博客网 时间:2024/05/17 00:07

就为这个问题翻遍国内外各大论坛  功夫不负有心人 终于在github上找到答案

原文地址:https://github.com/Adobe-Marketing-Cloud/app-sample-android-phonegap/wiki/Embed-Webview-in-Android-Fragment

废话不多说直接上代码

希望能帮助到需要的朋友

public class FragmentHtml extends Fragment implements CordovaInterface {private CordovaWebView webView = null;public void setSlidingMenu(SlidingMenu slidingMenu) {this.slidingMenu = slidingMenu;}public static FragmentHtml newInstance() {FragmentHtml fragment = new FragmentHtml();return fragment;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {context = inflater.getContext();LayoutInflater localInflater = inflater.cloneInContext(new CordovaContext(getActivity(), this));View rootView = localInflater.inflate(R.layout.fragment_html, container, false);webView = (CordovaWebView) rootView.findViewById(R.id.web_report);Config.init(getActivity());//webView.loadUrl(Config.getStartUrl());                webView.loadUrl("file:///android_asset/www/index.htm");                return rootView;}// Plugin to call when activity result is receivedprotected CordovaPlugin activityResultCallback = null;protected boolean activityResultKeepRunning;// Keep app running when pause is received. (default = true)// If true, then the JavaScript and native code continue to run in the// background// when another application (activity) is started.protected boolean keepRunning = true;private final ExecutorService threadPool = Executors.newCachedThreadPool();public Object onMessage(String id, Object data) {return null;}public void onDestroy() {super.onDestroy();if (webView.pluginManager != null) {webView.pluginManager.onDestroy();}}@Overridepublic ExecutorService getThreadPool() {return threadPool;}@Overridepublic void setActivityResultCallback(CordovaPlugin plugin) {this.activityResultCallback = plugin;}public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {this.activityResultCallback = command;this.activityResultKeepRunning = this.keepRunning;// If multitasking turned on, then disable it for activities that return// resultsif (command != null) {this.keepRunning = false;}// Start activitysuper.startActivityForResult(intent, requestCode);}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent intent) {super.onActivityResult(requestCode, resultCode, intent);CordovaPlugin callback = this.activityResultCallback;if (callback != null) {callback.onActivityResult(requestCode, resultCode, intent);}}private class CordovaContext extends ContextWrapper implements CordovaInterface {CordovaInterface cordova;public CordovaContext(Context base, CordovaInterface cordova) {super(base);this.cordova = cordova;}public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {cordova.startActivityForResult(command, intent, requestCode);}public void setActivityResultCallback(CordovaPlugin plugin) {cordova.setActivityResultCallback(plugin);}public Activity getActivity() {return cordova.getActivity();}public Object onMessage(String id, Object data) {return cordova.onMessage(id, data);}public ExecutorService getThreadPool() {return cordova.getThreadPool();}}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="0dp">    <org.apache.cordova.CordovaWebView        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:id = "@+id/rl_title"/></LinearLayout>



0 0