在Phonegap上恢复应用程序状态

ika*_*c56 2 android cordova

我开发了一个带有phonegap的Android应用程序,我的应用程序需要在切换按钮上切换,然后用户可以关闭应用程序并在后台服务中运行它.我的问题是当我关闭应用程序并再次返回应用程序时,它会切换到关闭切换按钮的初始状态.所以,我的问题是如何在android和phonegap上保存/重新调整app状态?

我已经在嵌入式PhoneGap应用程序中阅读了这个保存和恢复WebView,但我仍然不了解restoreFromPreferences()saveToPreferences(out)任何人都可以帮忙吗?

*抱歉英语不好

Dav*_*vid 5

正如@geet所说,localStorage是您存储数据并在以后检索数据所需的.根据Phonegap网站,localstorage提供对W3C Storage界面的访问.你可以在这里阅读:http://dev.w3.org/html5/webstorage/#the-localstorage-attribute.

要保存切换按钮位置,这就是我所做的:

<script>

function onDeviceReady() {

// Set togglebutton to false default
var togglebutton = window.localStorage.getItem("togglebutton");
if (togglebutton==null) window.localStorage.setItem("togglebutton", 'false');

// Set default state
if (window.localStorage.getItem("togglebutton")=='true') {
    $('#onoffswitch').attr("checked", "checked");
}

// Switch onoffswitch event
$('#onoffswitch').on('change', function(){
    if ($(this).prop('checked')) {
        window.localStorage.setItem("togglebutton", 'true');
    } else {
        window.localStorage.setItem("togglebutton", 'false');
    }
});

}

</script>
Run Code Online (Sandbox Code Playgroud)

在HTML中确保默认情况下不会选中您的切换元素(形成一个复选框):

<input type="checkbox" name="onoffswitch" id="onoffswitch">
Run Code Online (Sandbox Code Playgroud)

关于暂停和恢复,您可以在调用这些事件时执行操作.去做这个 :

<script>
    document.addEventListener("resume", yourCallbackFunction, false);
    document.addEventListener("pause", yourCallbackFunction, false);
</script>
Run Code Online (Sandbox Code Playgroud)