这是在app中嵌入cordova控件的学习,关于如何打包cordova-x
.x
.x
.jar可以参照上一篇博客
1、把打包好的jar包导入android工程的libs目录下
2、在布局文件中的加入如下cordovawebview控件
<org.apache.cordova.CordovaWebView
android:id="@+id/tutorialView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
3、相关Activity必须改造为实现接口CordovaInterface,相关使用可参照/framework/src/org/apache/cordova/CordovaActivity.java,简要代码如下:
publicclassCordovaViewTestActivityextendsActivityimplementsCordovaInterface{
4、app需要调用摄像头可以采用如下代码:
CordovaWebView cwv;
/* Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cwv =(CordovaWebView) findViewById(R.id.tutorialView);
Config.init(this);
cwv.loadUrl(Config.getStartUrl());
}
@Override
publicvoid setActivityResultCallback(CordovaPlugin plugin){
this.activityResultCallback = plugin;
}
/**
* Launch an activity for which you would like a result when it finished. When this activity exits,
* your onActivityResult() method is called.
*
* @param command The command object
* @param intent The intent to start
* @param requestCode The request code that is passed to callback to identify the activity
*/
publicvoid 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
/**
* Called when an activity you launched exits, giving you the requestCode you started it with,
* the resultCode it returned, and any additional data from it.
*
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
protectedvoid onActivityResult(int requestCode,int resultCode,Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
CordovaPlugin callback =this.activityResultCallback;
if(callback !=null){
callback.onActivityResult(requestCode, resultCode, intent);
}
}
5、记得要增加线程池,plugin需要跑在线程中:
@Override
复制
publice ExecutorService getThreadPool(){
return threadPool;
}
6、复制app的HTML 和JavaScript files 到 the Android 工程的/assets/www
目录下。
7、从/framework/res/xml
config.xml
文件到工程的/res/xml
directory