如何在Fragment中使用phonegap的CordovaWebView

2025-04-04 16:39:20
推荐回答(1个)
回答1:

 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;  
    }  
  
    @Override  
    public 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 received  
    protected 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();  
        }  
    }  
  
    @Override  
    public ExecutorService getThreadPool() {  
        return threadPool;  
    }  
  
    @Override  
    public 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  
        // results  
        if (command != null) {  
            this.keepRunning = false;  
        }  
        // Start activity  
        super.startActivityForResult(intent, requestCode);  
    }  
  
    @Override  
    public 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();  
        }  
    }  
  
}