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.preference.PreferenceManager;
import android.text.TextUtils;
import java.util.StringTokenizer;

public class Main {
    public static int[] getIntArray(Context ctx, String key) {
        return getIntArray(ctx, null, key);
    }

    public static int[] getIntArray(Context ctx, String prefsKey, String key) {
        StringTokenizer st = new StringTokenizer(
                (TextUtils.isEmpty(prefsKey) ? getPrefManager(ctx) : getPrefs(ctx, prefsKey)).getString(key, ""),
                ",");
        int[] savedList = new int[st.countTokens()];
        for (int i = 0; i < savedList.length; i++) {
            savedList[i] = Integer.parseInt(st.nextToken());
        }
        return savedList;
    }

    public static String getString(Context ctx, String key) {
        return getPrefManager(ctx).getString(key, "");
    }

    public static String getString(Context ctx, String prefsKey, String key) {
        return getPrefs(ctx, prefsKey).getString(key, "");
    }

    public static SharedPreferences getPrefManager(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());
    }

    public static SharedPreferences getPrefs(Context ctx, String prefsKey) {
        return ctx.getApplicationContext().getSharedPreferences(prefsKey, Context.MODE_PRIVATE);
    }
}