GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PreferencesProvider/README.md at master · mengdd/PreferencesProvider

Expand file tree

/

README.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
120 lines (86 loc) · 3.8 KB
/

README.md

Copy path

File metadata and controls

120 lines (86 loc) · 3.8 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

PreferencesProvider

Download

A replacement of Android SharedPreferences based on ContentProvider.

It solves the problem when we have to store/use preference values across process.

Inspired by the great work of grandcentrix/tray, but PreferencesProvider has a much simpler implementation.

Setup

repositories {
    jcenter() // If not already there
}

dependencies {
    compile 'com.mengdd.preferencesprovider:preferences-provider:0.2.0'
}

Set Authority

Library users must define their own authority for the content provider.

This is done by defining the preferences_provider_authority string value:

resValue "string", "preferences_provider_authority", "${applicationId}.preferencesprovider"

For example: In your app's build.gradle

defaultConfig {
    applicationId "com.ddmeng.preferencesprovider.sample"
    minSdkVersion 14
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    resValue "string", "preferences_provider_authority", "${applicationId}.preferencesprovider"
}

This is because every content provider needs an unique authority. If multiple apps use the library but they have same authority for a content provider, only one app can be installed on a device.

(See issue: #2).

So the library enforce the client users override the authority. If the authority is not defined, an IllegalStateException will be thrown. An authority with applicationId is recommended.

Usage

1. Create a module for your preferences.

A storage module is a group of preferences with a group name. You can create as many modules as you need.

myModule = new PreferencesStorageModule(this, "HelloModule1");

2. Put values

The key is a String, The value can be String, boolean, int, float or long.

myModule.put("stringKey", "Hello");
myModule.put("booleanKey1", true);
myModule.put("booleanKey2", false);
myModule.put("intKey", 123);
myModule.put("floatKey", 1.5f);
myModule.put("longKey", 123L);

3. Get values

We know we can pass a default value as parameter when you get SharedPreferences values. We can also do that using PreferencesProvider.

With Default Values:

String stringValue = myModule.getString("non-existing-key-2", "defaultValue");
boolean boolean1 = myModule.getBoolean("non-existing-booleanKey1", false);
boolean boolean2 = myModule.getBoolean("non-existing-booleanKey2", false);
int intValue = myModule.getInt("non-existing-intKey", 0);
float floatValue = myModule.getFloat("non-existing-floatKey", 0f);
long longValue = myModule.getLong("non-existing-longKey", 0L);

Without Default Values

You will have to add a try-catch block when you are trying to get value but not provide default value.

try {
    String stringValue = myModule.getString("stringKey");
    boolean boolean1 = myModule.getBoolean("booleanKey1");
    boolean boolean2 = myModule.getBoolean("booleanKey2");
    int intValue = myModule.getInt("intKey");
    float floatValue = myModule.getFloat("floatKey");
    long longValue = myModule.getLong("longKey");

    ...

} catch (ItemNotFoundException e) {
    e.printStackTrace();
}

Other operations

remove(), clear() and getAll() are also supported.

See: Sample Activity

Used Tools

BoD/android-contentprovider-generator is used to generated all the database and content provider codes.

Stetho is used to watch the database.

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.