-2.Fragment
public class MyFragmentNew extends BaseFragment implements CordovaInterface { public static MyFragmentNew newInstance() { MyFragmentNew fragment = new MyFragmentNew(); return fragment; } private String TAG = "MyFragmentNew"; private CordovaWebView myCordovaWebView; private Context mContext; // 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(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mContext = inflater.getContext(); View thisView = inflater.inflate(R.layout.home_pager_fragment_new, container, false); SystemWebView homeWebView = (SystemWebView) thisView .findViewById(R.id.cordov_webView_home); ConfigXmlParser parser = new ConfigXmlParser(); parser.parse(getActivity());// 这里会解析res/xml/config.xml配置文件 myCordovaWebView = new CordovaWebViewImpl(new SystemWebViewEngine( homeWebView));// 创建一个cordovawebview myCordovaWebView.init(new CordovaInterfaceImpl(getActivity()), parser.getPluginEntries(), parser.getPreferences());// 初始化 myCordovaWebView.loadUrl("file:///android_asset/www/index.html");// 加载网页 return thisView; } @Override 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 if (command != null) { this.keepRunning = false; } // Start activity super.startActivityForResult(intent, requestCode); } @Override public void setActivityResultCallback(CordovaPlugin plugin) { this.activityResultCallback = plugin; } @Override public void onDestroy() { super.onDestroy(); if (myCordovaWebView != null) { myCordovaWebView.handleDestroy(); } } @Override public Object onMessage(String id, Object data) { return null; } @Override public ExecutorService getThreadPool() { return threadPool; } @Override public void requestPermission(CordovaPlugin plugin, int requestCode, String permission) { // TODO Auto-generated method stub } @Override public void requestPermissions(CordovaPlugin plugin, int requestCode, String[] permissions) { // TODO Auto-generated method stub } @Override public boolean hasPermission(String permission) { // TODO Auto-generated method stub return false; } }3.需要在Fragment所在的Activity中重写onActivityResult()方法,将结果通知给自定义插件
public static CordovaPlugin mCordovaPlugin; /** * 为了将回调结果回传给Cordova插件 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub CordovaPlugin cordovaPlugin = this.mCordovaPlugin; if(cordovaPlugin != null){ cordovaPlugin.onActivityResult(requestCode,resultCode,data); } super.onActivityResult(requestCode, resultCode, data); }