在tizen Web应用程序中保存数据

时间:2014-12-24 08:20:55

标签: javascript tizen tizen-web-app

我写了一个应用程序,我需要将其数据保存在本地tizen mobile中。这是我写的代码:

    <script type="text/javascript">
        function saveData() {

            var a = document.getElementById('star');
            var b = document.getElementById('checkone');
            var c = document.getElementById('note');
            var d = document.getElementById('med');
            var e = document.getElementById('checktwo');
            var f = document.getElementById('wgt');
            var g = document.getElementById('wgt1');

            /* localStorage.getItem("a");
            localStorage.getItem("b");
            localStorage.getItem("c");
            localStorage.getItem("d");
            localStorage.getItem("e");
            localStorage.getItem("f");
            localStorage.getItem("g"); */

            /* Set the local storage item */
            if ("localStorage" in window) {
                localStorage.setItem(a, star);
                localStorage.setItem(b, checkone);
                localStorage.setItem(c, note);
                localStorage.setItem(d, med);
                localStorage.setItem(e, checktwo);
                localStorage.setItem(f, wgt);
                localStorage.setItem(g, wgt1);
                location.reload();
                alert("Date got")
            } else {
                alert("no localStorage in window");
            }

            /* Set the session storage item */
            if ("sessionStorage" in window) {
                sessionStorage.setItem(a, star);
                sessionStorage.setItem(b, checkone);
                sessionStorage.setItem(c, note);
                sessionStorage.setItem(d, med);
                sessionStorage.setItem(e, checktwo);
                sessionStorage.setItem(f, wgt);
                sessionStorage.setItem(g, wgt1);
                location.reload();
                alert("Data has been set")
            } else {
                alert("no sessionStorage in window");
            }

            window.onload = function() {
                var localhtml = "";
                var sessionhtml = "";

                /* Get the local storage item */
                for (var i = 0; i < localStorage.length; i++) {
                    localhtml += "<li>" + localStorage.key(i) + " : "
                            + localStorage.getItem(localStorage.key(i))
                            + "</li>";
                }
                document.getElementById("localStorageData").innerHTML = localhtml;

                /* Get the session storage item */
                for (var j = 0; j < sessionStorage.length; j++) {
                    sessionhtml += "<li>" + sessionStorage.key(j) + " : "
                            + sessionStorage.getItem(sessionStorage.key(j))
                            + "</li>";
                }
                document.getElementById("sessionStorageData").innerHTML = sessionhtml; 
            }
            tizen.filesystem.resolve('/opt/apps/trial/data', function(
                    dir) {
                documentsDir = dir;
                dir.listFiles(onsuccess, onerror);
            }, function(e) {
                console.log("Error" + e.message);
            }, "rw");

        }
    </script>

在这部分:

tizen.filesystem.resolve('/opt/apps/trial/data', function(
                        dir) {
                    documentsDir = dir;
                    dir.listFiles(onsuccess, onerror);
                }, function(e) {
                    console.log("Error" + e.message);
                }, "rw");

            }

trial是应用ID,我不了解如何从tizen手机获取应用ID。数据存储成功,但我无法检索存储的数据。

1 个答案:

答案 0 :(得分:3)

从问题的结尾开始。

要获取应用程序ID (和包ID),您可以使用tizen.application API

tizen.application.getCurrentApplication().appInfo.id // returns App Id
tizen.application.getCurrentApplication().appInfo.packageId // returns packageId

其他道具如:.name.version.iconPath等也可以在那里找到。

文件访问 - 您可以使用相对于虚拟路径(documentsimagesmusic,...)的位置来解析路径,或者使用绝对路径的file:///前缀。

请参阅SDK文档中的Filesystem API以供参考。

您还可以验证您是否已定义适当的权限:

http://tizen.org/privilege/filesystem.read
http://tizen.org/privilege/filesystem.write