First if you are unsure about how application preferences are setup on Android go read this tutorial and then come back I'll wait. Oh great, your back. Let's crack on with things.
Functionality
Okay, so the application preferences plugin will provide you with four methods that you can use to interact with the native Android preferences.
get(key, success, fail)
If the key exists in the preferences it will be returned as the single parameter of the success callback that you provide. If the key does not exist the failure callback will be executed with an error object with error.code set to 0 which means no property.
window.plugins.applicationPreferences.get(key, function(value) {
console.log("The value is = " + value);
}, function(error) {
console.log(JSON.stringify(error));
});
set(key, value, success, fail)
If the key exists in the preferences then value will be saved and your the success callback will be executed. If the key does not exist the failure callback will be executed with an error object with error.code set to 0 which again means no property.
window.plugins.applicationPreferences.set(key, value, function(value) {
console.log("set correctly");
}, function(error) {
console.log(JSON.stringify(error));
});
load(success, fail)
Calling load will have the native side loop through all the preferences creating a JSON object that will be returned as the single parameter of your success callback.
window.plugins.applicationPreferences.load(function(value) {
console.log("The object is = " + JSON.stringify(value));
}, function(error) {
console.log(JSON.stringify(error));
});
show(activity, success, fail)
Calling show passing in the class name of your PreferenceActivity class will cause the native Android GUI to be shown so your user can interact with the preferences. If the class name you pass in doesn't exists your failure callback will be called with an error object with error.code set to 1 which means no preferences activity.
window.plugins.applicationPreferences.show("com.simonmacdonald.prefs.QuickPrefsActivity");
which brings up a GUI that looks like this:
Installation
Installation of the Plugin follows along the common steps:
- Add the script tag to your html:
<script type="text/javascript" charset="utf-8" src="applicationPreferences.js"/>
- Copy the Java code into your project to the src/com/simonmacdonald/prefs folder.
- Create a preferences file named res/xml/preferences.xml following the Android specification.
- Finally you'll need to create a class that extends PreferenceActivity in order to be able to view/modify the preferences using the native GUI. Refer back to the tutorial I mentioned for more details.
Oh, and I'm pretty sure that Darren McEntee has already included this plugin in his Live Football on TV app which means this plugin is already in the wild.
Enjoy!


35 comments:
Nice! Again: thanks. :)
It wasn't Randy that made it
@Tue
Sorry for the incorrect attribution. I looked at the iOS directory when I should have been looking at the iPhone directory. I just fixed up the post to give you credit.
Hi,
I'm trying to install this plugin, but I can't run it because of 1 error:
"The method startActivity(Intent) is undefined for the type CordovaInterface"
There are also several warnings:
"The value of the field AppPreferences.LOG_TAG is not used"
The method getContext() from the type CordovaInterface is deprecated
"Map is a raw type. References to generic type Map should be parameterized"
"Iterator is a raw type. References to generic type Iterator should be parameterized"
I'm using Eclipse 4.2 on Fedora
Am I missing anything?
Thanks in advance,
Rafael
@Rafa
Upgrade to 2.0.0 and the error should go away. Don't worry about the warnings though.
Hi, Simon,
I can´t get started this AppPrefs plugin. I changed to cordova 2.0, set all things like the readme, create a preferences.xml and a QuickPrefsActivity like
package my.package;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class QuickPrefsActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
Eclipse don´t show me any error, but the emulator shows me nothing, too :(
Is there a working example?
Sorry for bad english
Hi, Simon ... it´s running ;) anyway, thanks
Hi Simon,
First of all, and knowing that I'm very belated in saying it, thanks a lot for your previous response. Because of several issues (including having to change my OS), I couldn't check it until a few minutes ago.
The error got fixed as you said. But now I'm having another error, this time related with de javascript code. The error is
Result of expression 'window.plugins' [undefined] is not an object at file ...js:196
The referred line is:
function dime_pref(key,defecto) {
window.plugins.applicationPreferences.get(key, function(value) {
console.log("El valor de la preferencia es = " + value);
return value;
}, function(error) {
console.log(JSON.stringify(error));
return defecto
});
}
Of course, your code (cordova.define(...) is previous to that line.
Once again, thank you very much in advance.
@Rafa
Someone did a pull request on my plugin. You should just be able to call appPreferences.get() now. No need for the window.plugins bit.
Simon, thanks for your quick response.
I must be doing something wrong. Now I get the error "ReferenceError: Can't find variable: appPreferences"
I'm not much experienced on javascript, so I'm not sure how this works, but I notice that appPreferences is defined inside (more precisely, at the end of them) the following lines:
cordova.define("cordova/plugin/applicationpreferences", function(require, exports, module) {
...
...
var appPreferences = new AppPreferences();
module.exports = appPreferences;
});
Am I missing anything?
Thanks again
@Rafa
Sorry, I made a mistake. here is the kinda code you want to add to your deviceready handler so you don't need to modify the rest of your html.
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.applicationPreferences) {
window.plugins.applicationPreferences = cordova.require("cordova/plugin/applicationpreferences");
}
It worked! Great!
Thanks a lot.
Using 2.0.0, I put the next code in the ondebiceReady event:
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.applicationPreferences) {
window.plugins.applicationPreferences = cordova.require("cordova/plugin/applicationpreferences");
}
applicationPreferences.get("enterTimes", function(value) {
console.log("The value is = " + value);
}, function(error) {
console.log(JSON.stringify(error));
});
And i got the error:
module cordova/plugin/applicationpreferences not found at undefined:0
Could you help me please??
Thank you very much!! :D
@52AMANTES
Make sure you are using the 2.0.0 applicationPreferences.js file and you are including it as one of your script tags.
Hi, Simon,
Here I'm again
I don't know if I declared victory too soon, or made something wrong afterwards...
I have this function:
function dime_pref(key,defecto) {
var valor = ''
window.plugins.applicationPreferences.get(key, function(value) {
console.log("El valor de la preferencia "+ key + " es = " + value);
valor = value;
}, function(error) {
console.log(JSON.stringify(error));
valor = defecto;
});
return valor
}
I call it in this way:
var avance = dime_pref('avance','100')
I see that it seems to work properly because the console log shows the proper value. But that value never gets the variable "avance". What may be wrong in what I'm doing?
I can add that I'm getting also a previous error in console.log:
TypeError: Result of expression 'cordova.exec' [undefined] is not a function. And it points at the line:
cordova.exec(success,fail,"applicationPreferences","get",[key]);
As always, thank you very much.
@Rafa
You are trying to call an asynchronous method in a synchronous way. What is happening is that the get method does not execute before you return valor. You'll need to set the value of avance in the success method of the get.
Hi Simon,
Thanks for your great information regarding the app prefs.
i want to open the application preferences from the ios app. i need more info about how to implement this method,
the last phongap plugin is not updated with the methods of show and load,
i only can use the set/get methods.
can you please tell me additional information?
thanks
@zaherrrr
Sorry I did not write the iOS plugin. When I was doing the Android version show/load seemed like good additions. You should add them to the iOS plugin and do a pull request.
You give very useful information iphone android application with that useful function. it is very good stuff but
I have this function:
function dime_pref(key,defecto) {
var valor = ''
window.plugins.applicationPreferences.get(key, function(value) {
console.log("El valor de la preferencia "+ key + " es = " + value);
valor = value;
}, function(error) {
console.log(JSON.stringify(error));
valor = defecto;
});
@Jack Smith
What is the question?
Simon, I've been traveling out of town and so far I have not been able to get back in front of the computer, so I don't remember well if I told you that I needed to get preferences values synchronously, because I would use them the page is being constructed (font size, background-color,...)
How should I do it?
Thank you very much in advance.
@Rafa
Sorry you are going to need to restructure your code to work with the async call. If you need these values for page construction you may want to:
1) Show a splash screen
2) wait for device ready
3) make a call to get the preferences
4) refresh the page with the preference values
5) hide the splash screen
Im new with phonegap and plugin and I try to use the appPreferences plugin but it keep giving me error: Class Not found.
Im new with phonegap and plugin and I try to use the appPreferences plugin but it keep giving me error: Class Not found
I even put the in the config.xml folder :S so I don't know what I'm missing to make it work
@Daniel Caymo
Hey, blogger won't accept xml in the comment so if you could post your manifest.xml and config.xml to a site like pastebin or gist then link back to it I could probably help.
this is the link: http://pastebin.com/Ks2vyUzD
http://pastebin.com/6uJnTpjz
Thx for replying and sorry If i send a lot of msg in the sametime because I don't know if I send it D: srry I'm really new to asking people in blog for help
@Daniel Caymo
The plugin line in your config.xml is wrong. The value of the plugin line should be:
"com.simonmacdonald.prefs.AppPreferences"
i try that alreadybut it still wouldnt.work ;S that why i change it n i thought it wouldnt matter as long it the path where the.file.is.located
@Daniel Caymo
Oh, it matters. The plugin line, the location of the file in the src folder and the package line in the the Java class must all match.
ah ok I try changing it tomorrow and see what happen thx you very for fast reply i let u know tomorrow if there any.change
I try to make the changes you told me and it still doesn't work and anw this is the whole application it a small one hope you be able to tell me what I'm doing wrong https://github.com/dandan28/test/down
This is my last question when i compile with eclipse through a virtual machine android the plugin works but when I try compiling it with https://build.phonegap.com/
builds the plugin doesn't work
@Daniel Caymo
Yeah, that is kinda essential information. You realize that PhoneGap Build does not support a ton of plugins and it does not support the AppPreferences plugin.
Hi,sorry I have another question again, when I try loading the ShowPreferenceAll automatically on body onload"ShowPreferenceAll();" it doesn't work?? because I want to show my Application preferences when my app load right away
@Daniel Caymo
That's because you are not waiting for the deviceready event. You won't be able to call and PhoneGap plugin or core API until after that event is fired. So listen for the event then show preferences.
Thx Simon it work now :D Thx for all those fast reply :D
Post a Comment