Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.SharedPreferences;

public class Main {
    /**
     * Add a {@code (key, value)} data into the given {@code sharedPreferences}.
     * Now, this method supports only {@code boolean} and {@link String}.
     */
    public static void updateSharedPreference(SharedPreferences sharedPreferences, String key, Object value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if (value.getClass() == Boolean.class) {
            editor.putBoolean(key, Boolean.class.cast(value));
        } else if (value.getClass() == String.class) {
            editor.putString(key, String.class.cast(value));
        } else if (value.getClass() == Integer.class) {
            editor.putInt(key, Integer.class.cast(value));
        } else {
            throw new IllegalArgumentException("value's type can be only Boolean or String.");
        }
        editor.commit();
    }
}