我的应用是使用条形码扫描程序插件的Cordova应用。
我使用的是MacOS,Cordova Android 6.1.2,条形码扫描程序插件6.0.6。
有些用户抱怨使用该应用时出现问题:
对于那些人来说,问题在于Android因为内存检测不足而决定破坏Cordova活动。
知道了,我决定使用 onPause / onResume Cordova事件来处理这种情况。
但我遇到问题,并不总是调用 onResume 。当Cordova活动没有被销毁时,onResume回调被很好地调用。但是当Cordova活动真正被破坏时(在配置“不要保持活动”首选项时模拟它),则不会调用onResume回调!
我的代码:
$(document).ready(function() {
if (console) {
console.log("--> évènement document.ready (JQuery) lancé");
}
document.addEventListener("deviceready", function() {
if (console) {
console.log("--> évènement deviceready (Cordova) lancé");
}
document.addEventListener('pause', impots.scan.onPause, false);
document.addEventListener('resume', impots.scan.onResume, false);
}, false);
});
impots.scan.onPause = function() {
console.log('Evenement pause ----------------');
impots.scan.debugAppState()
}
impots.scan.onResume = function(event) {
console.log('Evenement resume ----------------');
console.log('event ' + JSON.stringify(event));
var storedState = window.localStorage.getItem(impots.scan.APP_STORAGE_KEY);
// si storedState, c'est qu'Android a supprimé la Cordova Activity et il faut reconstituer l'UI
if (storedState && storedState.SPI) {
console.log('Chargement appStage a partir du LocalStorage')
impots.scan.updateModel(storedState.SPI)
}
if (event.pendingResult) {
if (event.pendingResult.pluginStatus === 'OK') {
console.log('Recuperation d\'un QR code suite à la suppression de la Cordova activity')
app.updateModel(event.pendingResult.result.text)
} else {
console.log('Probleme de réupération QR suite a la suppression de la Cordova activity')
}
}
impots.scan.updateUI()
}
在未销毁Cordova活动时记录:
15:15:07.156 30876 30876 I CordovaLog: Changing log level to VERBOSE(2)
15:15:07.157 30876 30876 I CordovaActivity: Apache Cordova native platform version 6.1.2 is starting
15:15:07.157 30876 30876 D CordovaActivity: CordovaActivity.onCreate()
15:15:07.267 30876 30876 I chromium: [INFO:library_loader_hooks.cc(140)] Chromium logging enabled: level = 0, default verbosity = 0
15:15:07.456 30876 30903 W cr_BindingManager: Cannot setInForeground() - never saw a connection for the pid: 30876
15:15:07.516 30876 30876 D SystemWebViewEngine: CordovaWebView is running on device made by: asus
15:15:07.575 30876 30876 D CordovaActivity: Started the activity.
15:15:07.575 30876 30876 D CordovaActivity: Resumed the activity.
15:15:07.938 30876 30876 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 30876
15:15:08.915 30876 30876 I chromium: [INFO:CONSOLE(12)] "mobileinit event triggered, initialization...", source: file:///android_asset/www/impots/js/impots-override.js (12)
15:15:11.061 30876 30876 I chromium: [INFO:CONSOLE(49)] "--> évènement document.ready (JQuery) lancé", source: file:///android_asset/www/impots/js/impots.js (49)
15:15:11.575 30876 30876 I chromium: [INFO:CONSOLE(70)] "--> évènement deviceready (Cordova) lancé", source: file:///android_asset/www/impots/js/impots.js (70)
15:15:19.319 30876 30876 D CordovaActivity: Paused the activity.
15:15:19.399 30876 30876 W cr_Ime : updateState: type [0->0], flags [0], show [false],
15:15:19.403 30876 30876 I chromium: [INFO:CONSOLE(256)] "Evenement pause ----------------", source: file:///android_asset/www/impots/js/impots-scan.js (256)
15:15:19.403 30876 30876 I chromium: [INFO:CONSOLE(230)] "appState {}", source: file:///android_asset/www/impots/js/impots-scan.js (230)
15:15:19.404 30876 30876 I chromium: [INFO:CONSOLE(234)] "Stockage de l'appState en LocalStorage", source: file:///android_asset/www/impots/js/impots-scan.js (234)
15:15:19.565 187 187 W SurfaceFlinger: couldn't log to binary event log: overflow.
15:15:19.655 616 2208 W ActivityManager: Unable to start service Intent { cmp=com.google.android.apps.photos/.cameraassistant.CameraAssistantService (has extras) } U=0: not found
15:15:19.835 202 208 E ANDR-PERF-LOCK: Failed to apply optimization for resource: 4 level: 0
15:15:19.838 194 652 W Camera2-ZslProcessor3: ZslProcessor3: Unable to find the android.request.pipelineMaxDepth, use default pipeline max depth 4
15:15:20.737 30876 30876 D CordovaActivity: Stopped the activity.
15:15:22.381 616 632 W ActivityManager: Activity pause timeout for ActivityRecord{fa630ef u0 fr.gouv.finances.smartphone.android/com.google.zxing.client.android.CaptureActivity t250 f}
15:15:23.077 202 208 E ANDR-PERF-LOCK: Failed to reset optimization for resource: 4 level: 0
15:15:23.101 30876 30876 D CordovaActivity: Incoming Result. Request code = 195543262
15:15:23.102 30876 30876 D CordovaInterfaceImpl: Sending activity result to plugin
15:15:23.102 30876 30876 D BarcodeScanner: dans onActivityResult
15:15:23.104 30876 30876 D CordovaActivity: Started the activity.
15:15:23.104 30876 30876 D CordovaActivity: Resumed the activity.
15:15:23.251 30876 30876 I chromium: [INFO:CONSOLE(266)] "Evenement resume ----------------", source: file:///android_asset/www/impots/js/impots-scan.js (266)
15:15:23.251 30876 30876 I chromium: [INFO:CONSOLE(267)] "event {"isTrusted":false,"action":"resume"}", source: file:///android_asset/www/impots/js/impots-scan.js (267)
简历事件很好。
当我在设备上使用“不要保留活动”并重试时:
15:20:43.396 1144 1355 W OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
15:20:43.396 1144 1355 W OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
15:20:44.800 1672 1672 W System : ClassLoader referenced unknown path: /data/app/fr.gouv.finances.smartphone.android-1/lib/arm
15:20:44.830 1672 1672 I CordovaLog: Changing log level to VERBOSE(2)
15:20:44.830 1672 1672 I CordovaActivity: Apache Cordova native platform version 6.1.2 is starting
15:20:44.830 1672 1672 D CordovaActivity: CordovaActivity.onCreate()
15:20:44.944 1672 1672 I chromium: [INFO:library_loader_hooks.cc(140)] Chromium logging enabled: level = 0, default verbosity = 0
15:20:45.160 1672 1703 W cr_BindingManager: Cannot setInForeground() - never saw a connection for the pid: 1672
15:20:45.221 1672 1672 D SystemWebViewEngine: CordovaWebView is running on device made by: asus
15:20:45.270 1672 1672 D CordovaActivity: Started the activity.
15:20:45.270 1672 1672 D CordovaActivity: Resumed the activity.
15:20:45.640 1672 1672 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 1672
15:20:46.605 1672 1672 I chromium: [INFO:CONSOLE(12)] "mobileinit event triggered, initialization...", source: file:///android_asset/www/impots/js/impots-override.js (12)
15:20:48.569 1672 1672 I chromium: [INFO:CONSOLE(49)] "--> évènement document.ready (JQuery) lancé", source: file:///android_asset/www/impots/js/impots.js (49)
15:20:49.026 1672 1672 I chromium: [INFO:CONSOLE(70)] "--> évènement deviceready (Cordova) lancé", source: file:///android_asset/www/impots/js/impots.js (70)
15:21:07.859 1672 1672 D CordovaActivity: Paused the activity.
15:21:07.929 1672 1672 W cr_Ime : updateState: type [0->0], flags [0], show [false],
15:21:07.940 1672 1672 I chromium: [INFO:CONSOLE(256)] "Evenement pause ----------------", source: file:///android_asset/www/impots/js/impots-scan.js (256)
15:21:07.943 1672 1672 I chromium: [INFO:CONSOLE(230)] "appState {}", source: file:///android_asset/www/impots/js/impots-scan.js (230)
15:21:07.943 1672 1672 I chromium: [INFO:CONSOLE(234)] "Stockage de l'appState en LocalStorage", source: file:///android_asset/www/impots/js/impots-scan.js (234)
15:21:08.103 187 187 W SurfaceFlinger: couldn't log to binary event log: overflow.
15:21:08.206 616 2094 W ActivityManager: Unable to start service Intent { cmp=com.google.android.apps.photos/.cameraassistant.CameraAssistantService (has extras) } U=0: not found
15:21:08.395 202 208 E ANDR-PERF-LOCK: Failed to apply optimization for resource: 4 level: 0
15:21:08.400 194 1102 W Camera2-ZslProcessor3: ZslProcessor3: Unable to find the android.request.pipelineMaxDepth, use default pipeline max depth 4
15:21:09.394 1672 1672 D CordovaActivity: Stopped the activity.
15:21:09.421 1672 1672 D CordovaActivity: CordovaActivity.onDestroy()
15:21:09.423 1672 1672 W cr_AwContents: WebView.destroy() called while WebView is still attached to window.
15:21:09.466 1672 1672 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 1672
15:21:09.468 1672 1672 W cr_AwContents: Application attempted to call on a destroyed WebView
15:21:09.468 1672 1672 W cr_AwContents: java.lang.Throwable
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.android_webview.AwContents.isDestroyed(AwContents.java:1265)
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.android_webview.AwContents.isDestroyedOrNoOperation(AwContents.java:1195)
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.android_webview.AwContents.getTitle(AwContents.java:2063)
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.android_webview.AwWebContentsDelegateAdapter.loadingStateChanged(AwWebContentsDelegateAdapter.java:280)
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:41)
15:21:09.468 1672 1672 W cr_AwContents: at android.os.Handler.dispatchMessage(Handler.java:102)
15:21:09.468 1672 1672 W cr_AwContents: at android.os.Looper.loop(Looper.java:148)
15:21:09.468 1672 1672 W cr_AwContents: at android.app.ActivityThread.main(ActivityThread.java:5417)
15:21:09.468 1672 1672 W cr_AwContents: at java.lang.reflect.Method.invoke(Native Method)
15:21:09.468 1672 1672 W cr_AwContents: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
15:21:09.468 1672 1672 W cr_AwContents: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
15:21:09.469 1672 1703 W cr_BindingManager: Cannot setInForeground() - never saw a connection for the pid: 1672
15:21:16.298 616 632 W ActivityManager: Activity pause timeout for ActivityRecord{4e505a3 u0 fr.gouv.finances.smartphone.android/com.google.zxing.client.android.CaptureActivity t251 f}
15:21:16.747 202 208 E ANDR-PERF-LOCK: Failed to reset optimization for resource: 4 level: 0
15:21:16.774 1672 1672 I CordovaLog: Changing log level to VERBOSE(2)
15:21:16.774 1672 1672 I CordovaActivity: Apache Cordova native platform version 6.1.2 is starting
15:21:16.774 1672 1672 D CordovaActivity: CordovaActivity.onCreate()
15:21:16.781 1672 1703 W cr_BindingManager: Cannot setInForeground() - never saw a connection for the pid: 1672
15:21:16.799 1672 1672 D SystemWebViewEngine: CordovaWebView is running on device made by: asus
15:21:16.836 1672 1672 D CordovaActivity: Started the activity.
15:21:16.838 1672 1672 D CordovaActivity: Incoming Result. Request code = 195543262
15:21:16.839 1672 1672 D BarcodeScanner: dans onRestoreStateForActivityResult
15:21:16.839 1672 1672 D CordovaInterfaceImpl: Sending activity result to plugin
15:21:16.839 1672 1672 D BarcodeScanner: dans onActivityResult
15:21:16.839 1672 1672 D CordovaActivity: Resumed the activity.
15:21:17.147 1672 1672 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 1672
15:21:17.497 1672 1672 I chromium: [INFO:CONSOLE(12)] "mobileinit event triggered, initialization...", source: file:///android_asset/www/impots/js/impots-override.js (12)
15:21:17.954 1672 1672 I chromium: [INFO:CONSOLE(49)] "--> évènement document.ready (JQuery) lancé", source: file:///android_asset/www/impots/js/impots.js (49)
15:21:18.311 1672 1672 I chromium: [INFO:CONSOLE(70)] "--> évènement deviceready (Cordova) lancé", source: file:///android_asset/www/impots/js/impots.js (70)
这次没有调用onResume。但是,我假设插件找到了一个结果,因为日志告诉“CordovaActivity:Incoming Result。请求代码= 195543262”和“CordovaInterfaceImpl:将活动结果发送到插件”
我发现了一个问题:“应用程序试图调用已销毁的WebView”。这只是一个警告,但它可能是没有调用onResume的原因,我真的不知道。
我的CordovaActivity就是这样:
public class MainActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
loadUrl(launchUrl);
}
@Override
public void onDestroy()
{
super.onDestroy();
}
}
任何想法都会受到赞赏!