【发布时间】:2014-09-05 18:38:51
【问题描述】:
已经一周了,我仍然在将 cordova 集成到 ana android webview 时遇到问题! 在 onPause 和 onResume 工作正常之前,但在集成后它们不再触发。 即使我在控制台中看到了这一点:
07-15 17:01:21.880: D/CordovaActivity(5635): Paused the application!
07-15 17:01:21.880: D/CORDOVA_ACTIVITY(5635): onPause
但是 opPause 函数中的代码没有执行!!我真的厌倦了。我尝试了一周不停地让它工作。
这是我的主要 java 文件:
package com.Snap.What;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import org.apache.cordova.*;
public class WhatSnap extends CordovaActivity implements CordovaInterface
{
private CordovaWebView cordova_webview;
private String TAG = "CORDOVA_ACTIVITY";
private final ExecutorService threadPool = Executors.newCachedThreadPool();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.cordova_layout);
cordova_webview = (CordovaWebView) findViewById(R.id.cordova_web_view);
// Config.init(this);
cordova_webview.loadUrl("file:///android_asset/www/index.html");
JavaScriptInterface jsInterface = new JavaScriptInterface(this);
cordova_webview.getSettings().setJavaScriptEnabled(true);
cordova_webview.addJavascriptInterface(jsInterface, "JSInterface");
}
public class JavaScriptInterface {
private Activity activity;
public JavaScriptInterface(Activity activiy) {
this.activity = activiy;
}
@JavascriptInterface
public void showLog(){
Log.v("blah", "blah blah");
}
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
public 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");
// }
}
我也尝试从 java 中评论 onPause 和 onresume 但我只在控制台中得到这个:
07-15 17:01:21.880: D/CordovaActivity(5635): Paused the application!
在我的 js 中,我有以下几行:
document.addEventListener("resume", onResume, false);
document.addEventListener("pause", punish, false);
以及不会执行的相应功能!
我真的不知道他们为什么不执行..请看一下,你有什么想法请告诉我。
【问题讨论】:
-
请确保在科尔多瓦的 deviceReady 事件之后添加侦听器。您可以将其添加到 deviceReady 事件处理程序中。
-
@Rupesh 我在 deviceready 函数中添加了它..
-
奇怪!!.. 我正在使用相同的,它的工作正常。我相信在你的情况下设备就绪处理程序正在执行,对吧?
-
@Rupesh 是的,伙计..只有 onPause 和 onReady 事件不起作用,其他工作正常..
标签: android cordova webview phonegap-plugins cordova-2.0.0