您的位置:首页 > 其它

在应用非正常退出时利用StateSaver来保存我们的数据

2015-06-10 06:53 316 查看
我们知道Ubuntu手机平台是一个单任务的系统。一个用户可以开启很多个应用,但是只有前台的应用是可以正在运行的应用。很多被推到后台的应用被驻存到内存中。如果有很多这样的被驻存的应用的话,内存迟早会被用完的。操作系统可以选择一些应用被杀死从而保证系统的正常运行。为了能够保证应用在退出时的状态,在Ubuntu系统上,我们设计了StateSaver这样的一个接口。它可以用来帮我们保存应用在非正常退出的状态,以便在应用重新启动后恢复以前的状态。

参照文章的设计,我们设计了如下的代码:

import QtQuick 2.0
import Ubuntu.Components 1.1

/*!
\brief MainView with a Label and Button elements.
*/

MainView {
id: mainview
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView"

// Note! applicationName needs to match the "name" field of the click manifest
applicationName: "statesaver.ubuntu"

/*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true

// Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false

width: units.gu(60)
height: units.gu(85)

Page {
id: mainpage
title: i18n.tr("State saver")

Rectangle {
id: root
anchors.fill: parent
color: "green"
StateSaver.properties: "color"

Column {
spacing: units.gu(2)
anchors.centerIn: parent

Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Change color"

onClicked: {
root.color = Qt.rgba(Math.random(1), Math.random(1), Math.random(1), 1);
}
}

Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Quit"

onClicked: Qt.quit()
}

}
}
}
}


运行我们的应用:



我们可以按下“Change color”来改变我们的颜色。当我们按下“Quit”后,退出应用。但是,等我们重新启动我们的应用后,在应用退出时的颜色还是当初始化的颜色绿色。也就是说我们在程序中使用的:

StateSaver.properties: "color"


没有帮我们保存下我们的颜色。

接下来,我们重新运行我们的应用,并调整好我们所需要的颜色,比如蓝色:



我们在电脑上打开我们的Terminal,并输入如下的命令:



我们可以看到我们的应用被无情地杀死了。我们再次在手机中打开我们的应用。我们可以看到应用启动后,颜色还是上次在被杀死时的蓝色,而不是应用在初始化的绿色。也就是说,颜色的值在进程被杀死的时候被保存下来了。

我们可以参阅文章来保存多个properties。

整个应用的代码在:https://github.com/liu-xiao-guo/statesaver
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: