我一直在联系 Phonegap 支持人员,以及比我更有经验的 Android 人员,以及知识渊博的移动 Web 应用程序开发人员,以确定我的应用程序中出现奇怪错误的原因。根据与我交谈过的人的说法,问题可能源于这些地方(Android、PhoneGap 插件或使用 jQuery mobile)。
这是包含 index.html 的文件夹,其中包含我的 Phonegap 应用程序代码: https ://github.com/galenthomasramos/EcigSurvey/tree/master/platforms/android/assets/www
这个应用程序的目标设备是摩托罗拉 Razr M。我使用本地通知插件来全天提供通知。有时单击通知时,我的 onResume 事件函数中的某些代码很晚才被触发。恢复应用程序后,我应该会立即收到并提醒,但我会在差不多一分钟后收到提醒。当我尝试登录我的应用程序(登录屏幕是我的应用程序中出现的第一页)我已正确输入密码但手机无法导航时,会出现更大的问题(相同的症状 - 非常晚的函数调用)到下一页。页面不可能只是重置而不导航到下一页——要么我会收到错误的用户输入警告并要求重试,要么应用程序应该成功导航到下一页。
我应该提到,这些错误似乎只在通过通知访问应用程序时发生,如果我从正在运行的应用程序堆栈恢复应用程序,则不会发生这种情况。
起初我认为可能是在我的应用程序被挂起时,或者可能是 Android 杀死了我的一些后台进程,这些变量在我的程序中被终止,这可能导致这种行为,但是在查看了与 Android 应用程序一起运行的应用程序之后观察内存使用情况和任何数量的垃圾收集,当这种情况发生时,Android 似乎并没有杀死任何东西。
我和其他人谈过 jQuery Mobile 可能是罪魁祸首(显然 jQuery Mobile 的行为因设备平台而异,而且我的 Nexus 4 上似乎没有收到这些错误)并且可能我的页面事件不起作用正确吗?
我的 onResume 事件函数(应该在恢复时触发警报):
function onResumeApp(){
//Check to see if there is a suspension time set, if so, check to see if that time has passed, if so, resume normal app behavior
if(resumeTime != null){
//in the case of delaying a notification, need to change currentState to active through a timeout function...
// otherwise causes error in how missing a delayed a notification counts more than once
if(moment().isAfter(resumeTime)){
currentState = states.ACTIVE;
displayAppStatus(states.ACTIVE);
resumeTime = null;
}
}
if (hasMissedNotification){
alert("You have missed " + missedNotifications + " random prompt survey(s). Please remember to take as many surveys as possible.");
hasMissedNotification = false;
missedNotifications = 0;
}
...
}
阅读暂停事件后可以导航到另一个页面吗?在这种情况下,我可以预见到在暂停事件侦听器中读取任何代码时,应用程序在后台会出现问题……再说一次,如果您不能编写任何代码,为什么 Phonegap 会包含暂停事件函数Javascript代码要使用吗?
我的 onPause 事件功能:
function userHasGoneIdle(){
//If user goes idle and has started a random prompt survey but has not finished it before going idle,
//Need to record a survey as being abandoned
if(startedRandomPromptSurvey && !finishedRandomPromptSurvey){
//If a user delays a notification, don't want to add to missing notifications file yet.
if(currentState != states.DELAYED){
hasMissedNotification = true;
addToMissedNotifications(1, moment().toDate().toString());
startedRandomPromptSurvey = false;
finishedRandomPromptSurvey = false;
}
//If a user clicks a notification (indicates that they have started a random prompt interview), delays notification,
//then misses the delayed notificaiton, it will register both an abandoned as well as a missed, when really they have only missed
//the last delayed notification
}
writingToRandomPromptInt = false;
$("#login-password").val('');
if(!wakeUpFiring)
$.mobile.navigate('#login');
else
$.mobile.navigate('#wakeup');
resetSurveyWidgets();
console.log('userHasGoneIdle');
}
然后我有一堆代码,由我使用的本地通知插件由事件侦听器函数执行。这些事件可以在任何时候发生,如果应用程序在前台或后台......当应用程序在后台时它们当然不会可靠地发生,我需要改变它的工作方式,但我想知道是否有任何这些事件函数中的代码可能会导致问题(我想说是,因为问题似乎只发生在通过通知访问应用程序时)
本地通知事件函数:
触发器:
window.plugin.notification.local.ontrigger = function (id, state, json) {
console.log('\nontrigger() #' + id + " State: " + state + " @" + moment().toString());// + ' @' + JSON.parse(json).date);
//if(id != serviceNotificationID){
if(Math.abs(id >= notificationCount - 1 || notificationCount == 0)){
console.log('id - notificationCount == 0:' + id + ' - ' + notificationCount + ' = ' + (id - notificationCount));
clearPriorNotifications(function(){
addNewNotifications(function(){
formNotificationString(writeToNotificationFile);
});
});
}
triggerTime = moment().toDate().toString();
if(id == 0)
didMissDelayedNotification = true;
if(id != -99){
playNotificationAudio();
}
//In the case that the user does not access app when an alarm goes off by clicking notification,
//Have app navigate to wakeup screen
else{
playAlarmAudio();
didMissWakeUp = true;
wakeUpFiring = true;
$.mobile.navigate("#wakeup");
}
setTimeout(function(){
if(id != -1 && id != -99){
//if Havent clicked on a notificaiton, and the notificaiton snt a delayed notificaiton,
if(didMissNotificationArr[parseInt(id)] && id != 0){
console.log("@onTrigger: didMissNotificationArr[" + id + "] = " + didMissNotificationArr[id]);
hasMissedNotification = true;
addToMissedNotifications(2, triggerTime);
window.plugin.notification.local.cancel(id);
}
//for missed delayed notifications, only log missed if not clicked and only cancel if not clicked (and therefore cnaceled)
else if(id == 0 && didMissDelayedNotification){
console.log("@onTrigger: didMissDelayedNotification = " + didMissDelayedNotification);
hasMissedNotification = true;
addToMissedNotifications(2, triggerTime);
window.plugin.notification.local.cancel(id);
}
}
//Need to set boolean to remain at #wakeup, regardless of whether or not the user leaves the app during
//wakeup alarm
else if(id == -99 && didMissWakeUp){
wakeUpFiring = false;
window.plugin.notification.local.cancel(id);
}
else if(id == -1)
window.plugin.notification.local.cancel(id);
}, (180 * 1000));
//}
};
点击:
window.plugin.notification.local.onclick = function(id, state, json){
console.log('\n clicking notification #' + id);
//notificationTime = JSON.parse(json).date;
//if(id != serviceNotificationID){
//When clicking on a delayed notification
if(id == 0){
$.mobile.navigate('#delay');
didMissDelayedNotification = false;
startedRandomPromptSurvey = true;
finishedRandomPromptSurvey = false;
}
//When clicking on a test notification
else if(id == -1){
$.mobile.navigate("#login");
takingTestSurvey = true;
startedRandomPromptSurvey = false;
finishedRandomPromptSurvey = false;
}
//When clicking on a wakeup notification
else if(id == -99){
$.mobile.navigate("#wakeup");
startedRandomPromptSurvey = false;
finishedRandomPromptSurvey = false;
}
//When clicking on a standard, untouched notification
else{
$.mobile.navigate('#delay');
startedRandomPromptSurvey = true;
finishedRandomPromptSurvey = false;
didMissNotificationArr[parseInt(id)] = false;
}
取消:
window.plugin.notification.local.oncancel = function (id, state, json) {
console.log('\n CANCELLING notification #' + id + " State: " + state);
//console.log("caller is " + arguments.callee.caller.toString());
if(id != -99){
if(my_media){
my_media.stop();
//my_media.reset();
my_media.release();
}
}
//If wake up alarm notificaiton:
else{
wakeUpFiring = false;
//$.mobile.navigate("#login");
if(my_alarm_media){
my_alarm_media.stop();
//my_media.reset();
my_alarm_media.release();
}
}
};
任何想法为什么我的应用程序在从通知中恢复时可能没有响应?