Compartilhar via


Preferences.UserNodeForPackage(Class) Method

Definition

<strong>WARNING:</strong> On Android, the Preference nodes corresponding to the "system" and "user" preferences are stored in sections of the file system that are inaccessible to apps.

[Android.Runtime.Register("userNodeForPackage", "(Ljava/lang/Class;)Ljava/util/prefs/Preferences;", "")]
public static Java.Util.Prefs.Preferences? UserNodeForPackage (Java.Lang.Class? c);
[<Android.Runtime.Register("userNodeForPackage", "(Ljava/lang/Class;)Ljava/util/prefs/Preferences;", "")>]
static member UserNodeForPackage : Java.Lang.Class -> Java.Util.Prefs.Preferences

Parameters

c
Class

the class for whose package a user preference node is desired.

Returns

the user preference node associated with the package of which c is a member.

Attributes

Remarks

<strong>WARNING:</strong> On Android, the Preference nodes corresponding to the "system" and "user" preferences are stored in sections of the file system that are inaccessible to apps. Further, allowing apps to set "system wide" preferences is contrary to android's security model.

Returns the preference node from the calling user's preference tree that is associated (by convention) with the specified class's package. The convention is as follows: the absolute path name of the node is the fully qualified package name, preceded by a slash ('/'), and with each period ('.') replaced by a slash. For example the absolute path name of the node associated with the class com.acme.widget.Foo is /com/acme/widget.

This convention does not apply to the unnamed package, whose associated preference node is &lt;unnamed&gt;. This node is not intended for long term use, but for convenience in the early development of programs that do not yet belong to a package, and for "throwaway" programs. Valuable data should not be stored at this node as it is shared by all programs that use it.

A class Foo wishing to access preferences pertaining to its package can obtain a preference node as follows:

static Preferences prefs = Preferences.userNodeForPackage(Foo.class);

This idiom obviates the need for using a string to describe the preferences node and decreases the likelihood of a run-time failure. (If the class name is misspelled, it will typically result in a compile-time error.)

Invoking this method will result in the creation of the returned node and its ancestors if they do not already exist. If the returned node did not exist prior to this call, this node and any ancestors that were created by this call are not guaranteed to become permanent until the flush method is called on the returned node (or one of its ancestors or descendants).

Java documentation for java.util.prefs.Preferences.userNodeForPackage(java.lang.Class<?>).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to