您的位置:首页 > 移动开发

Android 以webview的方式集成Dcloud 5+SDK 实现携带数据跳转原生界面

2017-01-06 12:49 711 查看
需求:在mainactivity中放入两个Fragment  其中一个是webview 的展示,支持两个Fragment切换

一、下载Dcloud SDK

地址:http://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/80

安卓的原生manactivity 和fragmeent 我就不在写了  

二、:

导入 jar文件  并添加依赖,文件在 sdk中的libs文件夹中 

需要导入的有

gallery.jar

my-imageloader.jar

nativeui.jar

nineoldandroids-2.4.0.jar

pdr.jar

ui.jar

三、再其中一个fragment的文件中开始初始化 并展示

1.重写 oncreat  方法:创建 rootview  用来添加 webview;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("ceshi","fragment.....1");
rootView = new FrameLayout(getActivity());
}

2.编写 class监听类实现   ICore.IcoreStatuListen

class WebviewModeListener implements ICore.ICoreStatusListener {

LinearLayout btns = null;
Activity activity = null;
ViewGroup mRootView = null;
IWebview webview = null;
ProgressDialog pd = null;
public WebviewModeListener(Activity activity, ViewGroup rootView) {
this.activity = activity;
mRootView = rootView;
btns = new LinearLayout(activity);
mRootView.setBackgroundColor(0xffffffff);
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

webview.onRootViewGlobalLayout(mRootView);
}
});
}

/**
* 5+内核初始化完成时触发
* */
@Override
public void onCoreInitEnd(ICore coreHandler) {
// 设置单页面集成的appid
String appid = "TestAppid";
// 单页面集成时要加载页面的路径,可以是本地文件路径也可以是网络路径
String url = "file:///android_asset/ceshi.html";//本地文件路径

webview = SDK.createWebview(activity, url, appid, new IWebviewStateListener() {
@Override
public Object onCallBack(int pType, Object pArgs) {
switch (pType) {
case IWebviewStateListener.ON_WEBVIEW_READY: Log.i("ceshi","执行此方法+onCallback");
// 准备完毕之后添加webview到显示父View中,设置排版不显示状态,避免显示webview时,html内容排版错乱问题
((IWebview) pArgs).obtainFrameView().obtainMainView().setVisibility(View.INVISIBLE);
SDK.attach(mRootView, ((IWebview) pArgs));
break;
case IWebviewStateListener.ON_PAGE_STARTED: Log.i("ceshi","执行此方法+onCallback");
// 首页面开始加载事件
break;
case IWebviewStateListener.ON_PROGRESS_CHANGED: Log.i("ceshi","执行此方法+onCallback");
// 首页面加载进度变化
break;
case IWebviewStateListener.ON_PAGE_FINISHED:
// 页面加载完毕,设置显示webview
webview.obtainFrameView().obtainMainView().setVisibility(View.VISIBLE);
break;
}
return null;
}
});

final WebView webviewInstance = webview.obtainWebview();
// 监听返回键
webviewInstance.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (webviewInstance.canGoBack()) {
webviewInstance.goBack();
return true;
}
}
return false;
}
});
}

// 5+SDK 开始初始化时触发
@Override
public void onCoreReady(ICore coreHandler) {
try {
// 初始化5+ SDK,
// 5+SDK的其他接口需要在SDK初始化后才能調用
SDK.initSDK(coreHandler);
// 当前应用可使用全部5+API
SDK.requestAllFeature();

} catch (Exception e) {
Log.i("ceshi","异常");
e.printStackTrace();
}

}

/**
*     // 通过代码注册扩展插件的示例
* private void regNewApi() {
*       // 扩展插件在js层的标识
*    String featureName = "T";
*    // 扩展插件的原生类名
*    String className = "com.HBuilder.integrate.webview.WebViewMode_FeatureImpl";
*    // 扩展插件的JS层封装的方法
*    String content = "(function(plus){function test(){return plus.bridge.execSync('T','test',[arguments]);}plus.T = {test:test};})(window.plus);";
*    // 向5+SDK注册扩展插件
*    SDK.registerJsApi(featureName, className, content);
* }
**/

@Override
public boolean onCoreStop() {
// TODO Auto-generated method stub
return false;
}
}


3.重写oncreatview方法,

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View rootview=inflater.inflate(R.layout.fragment_2,container,false);

// 创建5+内核运行事件监听
WebviewModeListener wm = new WebviewModeListener(getActivity(), rootView);
// 初始化5+内核
mEntryProxy = EntryProxy.init(getActivity(), wm);
// 启动5+内核
mEntryProxy.onCreate(getActivity(), savedInstanceState, SDK.IntegratedMode.WEBVIEW, null);
Log.i("ceshi","fragment.....2");

return rootView;
}


3.在fragment中重写方法,不需要的可以不写
 

@Override
public void onDestroy() {
super.onDestroy();

mEntryProxy.onStop(getActivity());
}

@Override
public void onResume() {
super.onResume();
mEntryProxy.onResume(getActivity());

}

@Override
public void onPause() {
super.onPause();
mEntryProxy.onPause(getActivity());

}


  Fragment的完整代码

public class Fragment_2 extends Fragment{

EntryProxy mEntryProxy = null;
FrameLayout rootView;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i("ceshi","fragment.....1"); rootView = new FrameLayout(getActivity()); }

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// View rootview=inflater.inflate(R.layout.fragment_2,container,false);

// 创建5+内核运行事件监听
WebviewModeListener wm = new WebviewModeListener(getActivity(), rootView);
// 初始化5+内核
mEntryProxy = EntryProxy.init(getActivity(), wm);
// 启动5+内核
mEntryProxy.onCreate(getActivity(), savedInstanceState, SDK.IntegratedMode.WEBVIEW, null);
Log.i("ceshi","fragment.....2");

return rootView;
}

@Override
public void onDestroy() {
super.onDestroy();
Log.i("ceshi","fragment.....onDestroy");
mEntryProxy.onStop(getActivity());
}

@Override
public void onResume() {
super.onResume();
mEntryProxy.onResume(getActivity());
Log.i("ceshi","fragment.....onresume");
}

@Override
public void onPause() {
super.onPause();
mEntryProxy.onPause(getActivity());
Log.i("ceshi","fragment.....onpause");
}
}
class WebviewModeListener implements ICore.ICoreStatusListener {

LinearLayout btns = null;
Activity activity = null;
ViewGroup mRootView = null;
IWebview webview = null;
ProgressDialog pd = null;
public WebviewModeListener(Activity activity, ViewGroup rootView) {
this.activity = activity;
mRootView = rootView;
btns = new LinearLayout(activity);
mRootView.setBackgroundColor(0xffffffff);
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.i("ceshi",webview+"");
webview.onRootViewGlobalLayout(mRootView);
}
});
}

/**
* 5+内核初始化完成时触发
* */
@Override
public void onCoreInitEnd(ICore coreHandler) {
// 设置单页面集成的appid
String appid = "TestAppid";
// 单页面集成时要加载页面的路径,可以是本地文件路径也可以是网络路径
String url = "file:///android_asset/ceshi.html";
Log.i("ceshi","fragment.....3");
webview = SDK.createWebview(activity, url, appid, new IWebviewStateListener() {
@Override
public Object onCallBack(int pType, Object pArgs) {
switch (pType) {
case IWebviewStateListener.ON_WEBVIEW_READY: Log.i("ceshi","执行此方法+onCallback");
// 准备完毕之后添加webview到显示父View中,设置排版不显示状态,避免显示webview时,html内容排版错乱问题
((IWebview) pArgs).obtainFrameView().obtainMainView().setVisibility(View.INVISIBLE);
SDK.attach(mRootView, ((IWebview) pArgs));
break;
case IWebviewStateListener.ON_PAGE_STARTED: Log.i("ceshi","执行此方法+onCallback");
// 首页面开始加载事件
break;
case IWebviewStateListener.ON_PROGRESS_CHANGED: Log.i("ceshi","执行此方法+onCallback");
// 首页面加载进度变化
break;
case IWebviewStateListener.ON_PAGE_FINISHED:
// 页面加载完毕,设置显示webview
webview.obtainFrameView().obtainMainView().setVisibility(View.VISIBLE);
break;
}
return null;
}
});

final WebView webviewInstance = webview.obtainWebview();
// 监听返回键
webviewInstance.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (webviewInstance.canGoBack()) {
webviewInstance.goBack();
return true;
}
}
return false;
}
});
}

// 5+SDK 开始初始化时触发
@Override
public void onCoreReady(ICore coreHandler) {
try {
// 初始化5+ SDK,
// 5+SDK的其他接口需要在SDK初始化后才能調用
SDK.initSDK(coreHandler);
// 当前应用可使用全部5+API
SDK.requestAllFeature();
Log.i("ceshi","正常");
Log.i("ceshi","fragment.....4");
} catch (Exception e) {
Log.i("ceshi","异常");
e.printStackTrace();
}
Log.i("ceshi","未知");
}

/**
* // 通过代码注册扩展插件的示例
* private void regNewApi() {
* // 扩展插件在js层的标识
* String featureName = "T";
* // 扩展插件的原生类名
* String className = "com.HBuilder.integrate.webview.WebViewMode_FeatureImpl";
* // 扩展插件的JS层封装的方法
* String content = "(function(plus){function test(){return plus.bridge.execSync('T','test',[arguments]);}plus.T = {test:test};})(window.plus);";
* // 向5+SDK注册扩展插件
* SDK.registerJsApi(featureName, className, content);
* }
**/

@Override
public boolean onCoreStop() {
// TODO Auto-generated method stub
return false;
}
}

四、接下来就是重要的配置 

1.拷贝sdk中的data文件,路径为HbuilderSDK\Android-SDK\SDK\assets到  项目中的assets文件夹下

2.在项目中的java文件夹下建一个包 名为:io.dcloud,名字要一样,将HbuilderSDK\Android-SDK\SDK\src\io\dcloud中的RInformation.java

文件拷贝进去。

3.在清单文件中添加权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

至此  文件就集成成功  可以展示

未完。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android webview 5+SDK