百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

PowerPreference

> 数据库
开源

一个强大的库,可用于控制和简化 Android 中共享偏好设置的使用。

100 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

一个强大的库,可用于控制和简化 Android 中共享偏好设置的使用。

Power Preference

A Powerful library to control and simplify the usage of shared preference in Android.

Download

Step 1. Add the JitPack repository to your build file

gradle
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

gradle
implementation 'com.github.AliAsadi:PowerPreference:2.1.1'

Getting started

Initialize the library in your Application.onCreate() method:

java
PowerPreference.init(this);

Access the Preference File

There are two ways to access the preference file:

  1. Default File

This is the default file provided by the library.

java
Preference preference = PowerPreference.getDefaultFile();
  1. Custom File

When you choose a file name, a new file with that name will be created.

java
Preference preference = PowerPreference.getFileByName("preferenceName");

Save Data

The library can store data in two ways. asynchronously and synchronously.

Also, The library can store any type of data.

Integer, Long, Float, Double, String, ArrayList, Map, Object are all valid data types.

Asynchronous

To write data to a preference file asynchronously, use methods that begin with the put prefix.

The thread you're working on will not be blocked.

  • Insert single value
java
PowerPreference.getDefaultFile().putString("key",value);
  • Insert multiple values
java
PowerPreference.getDefaultFile()
        .putString("key", value)
        .putObject("key", value)
              .
              .
        .putMap("key", value);

Synchronous

To write data to a preference file synchronous, use methods that begin with the set prefix.

Until the save operation's result is received, the thread you're working on will be blocked.

java
boolean result = PowerPreference.getDefaultFile().setString("key",value);

Set can also be called with other types, such as setBoolean() and setObject().

Read Data

To retrieve data from a preference file, use methods that begin with the get prefix.

  1. String
java
String value = PowerPreference.getDefaultFile().getString("key", defValue);
  1. Object
java
Object value = PowerPreference.getDefaultFile().getObject("key", Object.class, defValue);
  1. Array
java
ArrayList value = PowerPreference.getDefaultFile().getObject("key", Object[].class, defValue);
  1. Map
java
MapStructure structure = MapStructure.create(HashMap.class, String.class, Object.class);
HashMap value = PowerPreference.getDefaultFile().getMap("key", structure);

Note:

If you don't specify a default value when calling get and the key you're looking for doesn't exist, the library will return default value from the list below.

For Example:

java
String value = PowerPreference.getDefaultFile().getString("key");

If you call get string without specifying a default value and the key does not exist, the library will return an empty string as a default value.

Type Default
Integer, Long, Float, Double 0
String ""
Object null

You also can choose a default value for each key in you preference file by seDefaults() method see the defaults section for more.

Remove Data

  • Synchronous
java
PowerPreference.getDefaultFile().remove("key");
  • Asynchronous
java
PowerPreference.getDefaultFile().removeAsync("key");

Clear Data

1. Clear data from specefic file

  • Synchronous
java
PowerPreference.getDefaultFile().clear();
  • Asynchronous
java
PowerPreference.getDefaultFile().clearAsync();

2. Clear data from all files

  • Synchronous
java
PowerPreference.clearAllData();
  • Asynchronous
java
PowerPreference.clearAllDataAsync();

Get Data

  1. Get all data for a specefic file
java
Map data = PowerPreference.getDefaultFile().getData();
  1. Get all data for all files in the app.
java
List data = PowerPreference.getAllData()

Default Values

Set Default value to be used when reading from shared preference, You can use a different defaults value for every preference file.

There is two option using:

  1. XML
xml

    
        key
        true
    

    
        key
        Hello World!
    
    
    
        key
        5
    
    
    //...etc
    
java
PowerPreference.getDefaultFile().setDefaults(R.xml.preferences_defaults)
  1. HashMap
java
Map defaults = new HashMap<>();
defaults.put("key", true);
defaults.put("key", "Hello World!");
defaults.put("key", 123);
defaults.put("key", 111L);
defaults.put("key", 1.1f);
defaults.put("key", 1.111d);
defaults.put("key", new Object());
java
PowerPreference.getDefaultFile().setDefaults(hashMap);

Preference Debbuger

Using the preference debugger, you can easily see and edit all of your app's preferences data.

java
PowerPreference.showDebugScreen();

License

Copyright 2018 Ali Asadi.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javaandroidandroid-debug-databaseandroid-developmentandroid-library

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库