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;

import android.text.TextUtils;
import com.google.gson.Gson;

public class Main {

    public static <T> T getObject(String preName, Context context, String key, Class<T> clazz) {
        SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
        String str = pre.getString(key, "");
        return TextUtils.isEmpty(str) ? null : new Gson().fromJson(str, clazz);
    }

    public static String getString(String preName, Context context, String key) {
        SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
        return pre.getString(key, "");
    }
}