如何在phonegap中添加共享首选项?

时间:2013-09-26 06:35:42

标签: android cordova phonegap-build

我无法找到向我的应用添加共享偏好设置的方法,

任何人都可以给我一个教程链接如何在Windows环境中这样做。

如何将服务添加到phonegap?

2 个答案:

答案 0 :(得分:0)

我不确定共享偏好是什么意思。您是指如何为不同用户存储用户首选项?如果是这样,您需要将每个首选项存储在设备数据库中,并将其拉出以供用户使用该应用程序。请查看this,了解如何与数据库进行交互。

“在Windows环境中如何操作”是什么意思?如果您的意思是如何为Windows设备应用程序执行此操作,则无关紧要。对Windows进行操作与为iOS和Android进行操作的代码相同。但是,如果你的意思是如何在Windows环境中开发代码,那也无所谓。由于您使用了标签“phonegap-build”,我假设您使用的是PhoneGap Build而不是直接使用Cordova。使用PhoneGap Build,您可以在任何设备上开发HTML / CSS / JS,将其压缩并上传到build.phonegap.com,它将为您构建应用程序,无论编写代码的操作系统是什么。

如何将服务添加到phonegap?从技术上讲,你不要。您使用JavaScript而不是为phonegap为您的app构建服务。就个人而言,我更喜欢使用jQuery和json。看一下使用jQuery访问json web服务的example/documentation

将来,您应该在此处发布您尝试过的内容以及结果。通常,人们不会帮助,除非你表明你曾试图帮助自己。你在一个美好的早晨抓住了我:)

答案 1 :(得分:-1)


这是您的代码示例,您可以查看更多here

    //Create/Open a SharedPreferences(file xml) with name: "filename" and mode:MODE_PRIVATE(only your app read/write) 
    SharedPreferences shared = getSharedPreferences("filename", Context.MODE_PRIVATE);
    //Create editor to set String or anything into SharedPreferences file
    SharedPreferences.Editor editor = shared.edit();
    //Set Content for SharedPreferences file 
    editor.putString("user","content set for key 'user' ");
    editor.putString("pass","content set for key 'pass' ");
    //Important. Commit after set value. Same commit transaction in SQL 
    editor.commit();
    //Get content from SharedPreferences file
    String username = shared.getString("user", "defaul_value if key user not available");
    String password = shared.getString("pass", "defaul_value if key user not available");

在android中服务。更多细节here