android 中集成Cordova 一般情况下 activity继承Cordova 的CordovaActivity 就可以使用CordovaActivity内部的appView也就是 WebView来显示加载的页面 但有时候需要在Fragment中使用webView来加载网页 : 下面的frament 中使用了 cordova的systemWebView来加载 在布局文件中 使用systemWebView来加载 但是在fragment中需要通过CordovaWebView来包裹
代码:
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.efeiyi.demo.utils.LogUtils;
import org.apache.cordova.ConfigXmlParser;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaPreferences;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewImpl;
import org.apache.cordova.PluginEntry;
import org.apache.cordova.engine.SystemWebView;
import org.apache.cordova.engine.SystemWebViewEngine;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
* Created by Administrator on 2018/4/16.
* Cordova fragment 基类
*/
public abstract class BaseFragment extends Fragment implements CordovaInterface {
public static final String TAG = BaseFragment.class.getCanonicalName();
protected Context mContext;
protected View mRootView;
protected ConfigXmlParser mParser;
protected CordovaPreferences preferences;
protected String launchUrl;
protected MyCordovaInterfaceImpl mCordovaInterfaceImpl;
protected ArrayList<PluginEntry> mPluginEntries;
protected SystemWebView systemWebView;
protected CordovaWebView mWebView;
private Unbinder unbinder;
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadConfig();
}
private void loadConfig() {
mParser = new ConfigXmlParser();
mParser.parse(this.getActivity());
preferences = mParser.getPreferences();
preferences.setPreferencesBundle(getActivity().getIntent().getExtras());
launchUrl = mParser.getLaunchUrl();
mPluginEntries = mParser.getPluginEntries();
mParser.parse(mContext);
mCordovaInterfaceImpl = new MyCordovaInterfaceImpl(getActivity());
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mRootView == null) {
LayoutInflater localInflater = inflater.cloneInContext(new CordovaContext(mContext, this));
mRootView = localInflater.inflate(attachLayoutRes(), null);
unbinder = ButterKnife.bind(this, mRootView);
systemWebView = mRootView.findViewById(attachWebviewIdRes());
// 构建systemWebView
mWebView = new CordovaWebViewImpl(new SystemWebViewEngine(systemWebView));
// webView初始化
mWebView.init(mCordovaInterfaceImpl, mParser.getPluginEntries(), preferences);
}
return mRootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mWebView.loadUrl(loadWebViewUrl());
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
setUpListener();
}
/**
* 监听
*/
protected abstract void setUpListener();
/**
* 初始化数据
*/
protected abstract void initData();
/**
* 返回webView的网址
*/
protected abstract String loadWebViewUrl();
/**
* 获取webView页面
*/
@IdRes
protected abstract int attachWebviewIdRes();
/**
* 获取布局文件
*/
@LayoutRes
protected abstract int attachLayoutRes();
@Override
public void onDestroyView() {
super.onDestroyView();
if (unbinder != null) {
unbinder.unbind();
}
if (null != mWebView) {
mWebView.handleDestroy();
}
}
// -------------------- webView 接口实现的方法 -----------------------
protected static CordovaPlugin mCordovaPlugin;
protected boolean keepRunning = true;
private final ExecutorService threadPool = Executors.newCachedThreadPool();
protected boolean activityResultKeepRunning;
@Override
public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
LogUtils.d(TAG, "BaseFragment webView 版本 startActivityForResult : ");
mCordovaPlugin = command;
this.activityResultKeepRunning = this.keepRunning;
if (command != null) {
this.keepRunning = false;
}
super.startActivityForResult(intent, requestCode);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//get plugin
CordovaPlugin plugin = this.mCordovaPlugin;
if (plugin != null) {
plugin.onActivityResult(requestCode, resultCode, data);
}
mCordovaInterfaceImpl.onActivityResult(requestCode, resultCode, data);
}
@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
this.mCordovaPlugin = plugin;
}
@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) {
}
@Override
public void requestPermissions(CordovaPlugin plugin, int requestCode, String[] permissions) {
}
@Override
public boolean hasPermission(String permission) {
return false;
}
}
需要新建一个 CordovaContext 来更改默认的content
CordovaContext:
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import java.util.concurrent.ExecutorService;
/**
* Created by Administrator on 2018/4/10.
*
*/
public class CordovaContext extends ContextWrapper implements CordovaInterface {
public static final String TAG = CordovaContext.class.getSimpleName();
CordovaInterface cordova;
public CordovaContext(Context context, CordovaInterface fragment) {
super(context);
this.cordova = fragment;
}
@Override
public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
cordova.startActivityForResult(command, intent, requestCode);
}
@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
cordova.setActivityResultCallback(plugin);
}
@Override
public Activity getActivity() {
return cordova.getActivity();
}
@Override
public Context getContext() {
return cordova.getContext();
}
@Override
public Object onMessage(String id, Object data) {
return cordova.onMessage(id, data);
}
@Override
public ExecutorService getThreadPool() {
return cordova.getThreadPool();
}
@Override
public void requestPermission(CordovaPlugin plugin, int requestCode, String permission) {
}
@Override
public void requestPermissions(CordovaPlugin plugin, int requestCode, String[] permissions) {
}
@Override
public boolean hasPermission(String permission) {
return false;
}
}
把fragment 作为参数传如构造器 ;
还需要自定义CordovaInterfaceImpl 来达到 原生和H5的交互 ;
import android.app.Activity;
import org.apache.cordova.CordovaInterfaceImpl;
/**
* Created by Administrator on 2018/4/13.
*/
public class MyCordovaInterfaceImpl extends CordovaInterfaceImpl {
public MyCordovaInterfaceImpl(Activity activity) {
super(activity);
}
}
在上面的fragment中需要重写 onActivityResult() 方法 , 在其中使用
mCordovaInterfaceImpl.onActivityResult(requestCode, resultCode, data);
来调用 建议看看CordovaActivity是如何实现的 .
网友评论