Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;
import android.content.SharedPreferences;

public class Main {

    public static <T> T getData(Context context, String fileName, String key, Class T) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        T result;
        if (String.class.isAssignableFrom(T)) {
            result = (T) sharedPreferences.getString(key, "");
        } else if (Integer.class.isAssignableFrom(T)) {
            result = (T) Integer.valueOf(sharedPreferences.getInt(key, 0));
        } else if (Float.class.isAssignableFrom(T)) {
            result = (T) Float.valueOf(sharedPreferences.getFloat(key, 0));
        } else if (Long.class.isAssignableFrom(T)) {
            result = (T) Long.valueOf(sharedPreferences.getLong(key, 0));
        } else {
            result = (T) Boolean.valueOf(sharedPreferences.getBoolean(key, false));
        }
        return result;
    }
}