【发布时间】:2014-09-01 21:21:10
【问题描述】:
我只需要几分钟就有人告诉我这些步骤对于在 android webview 中实现 cordova 是否正确:
编辑:好的,我终于让它工作了,这些是正确的步骤:
1)我创建项目:cordova create hello com.example.hello HelloWorld并进入文件夹
2) cordova platform add android, cordova run android (cordova.jar 已创建) => 应用程序已启动 => 显示设备已准备就绪
3)我使用以下代码在“/res/layout”中创建了一个cordova_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<org.apache.cordova.CordovaWebView
android:id="@+id/cordova_web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
4)导入项目(作为eclipse中的“现有项目”)并在导入后添加到主java文件中:
public class HelloWorld extends Activity implements CordovaInterface {
private CordovaWebView cordova_webview;
private String TAG = "CORDOVA_ACTIVITY";
private final ExecutorService threadPool = Executors.newCachedThreadPool();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cordova_layout);
cordova_webview = (CordovaWebView) findViewById(R.id.cordova_web_view);
// Config.init(this);
String url = "file:///android_asset/www/index.html";
cordova_webview.loadUrl(url, 10000);
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onDestroy() {
super.onDestroy();
if (this.cordova_webview != null) {
this.cordova_webview
.loadUrl("javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};");
this.cordova_webview.loadUrl("about:blank");
cordova_webview.handleDestroy();
}
}
@Override
public Activity getActivity() {
return this;
}
@Override
public ExecutorService getThreadPool() {
return threadPool;
}
@Override
public Object onMessage(String message, Object obj) {
Log.d(TAG, message);
if (message.equalsIgnoreCase("exit")) {
super.finish();
}
return null;
}
@Override
public void setActivityResultCallback(CordovaPlugin cordovaPlugin) {
Log.d(TAG, "setActivityResultCallback is unimplemented");
}
@Override
public void startActivityForResult(CordovaPlugin cordovaPlugin,
Intent intent, int resultCode) {
Log.d(TAG, "startActivityForResult is unimplemented");
}
}
注意:活动名称必须与 manifest.xml 中的名称匹配
希望对您有所帮助。 祝你有美好的一天!
【问题讨论】:
-
“Private final ExecutorService threadPool =Executors.newCachedThreadPool()”的来源和用途是什么?
-
@Regent 如果我只添加 phonegap 页面上的代码::@Override public ExecutorService getThreadPool() { return threadPool;它给了我错误,我在某处读到我也需要添加这个以便它可以工作..否则,如果我点击错误,“返回线程池”将转换为 getthreadpool() 之类的东西
-
能否请您发布您的 logcat 错误。
-
@SiddharthVyas 当然!我编辑了我的问题
-
@AlexStanese : 你有没有添加任何 jar 文件?
标签: android cordova webview cordova-2.0.0 cordova-plugins