Java tutorial
//package com.java2s; //License from project: Open Source License import android.content.SharedPreferences.Editor; public class Main { private static final String MISSING_EDITOR = "Missing editor"; private static final String MISSING_VALUES = "Missing values"; /** * Adds values to a SharedPreferences Editor object. Key,value * pairs are expected to come in the values array one after another * such as [key1, value1, key2, value2], etc. This is a side effect * of the Ruby to Java communication through ADB. * * @param editor Editor to which add the values to. * @param values Array of key/value pairs. * * @throws IllegalArgumentException if missing editor or values. */ public static void setPreferences(Editor editor, String[] values) { if (editor == null) { throw new IllegalArgumentException(MISSING_EDITOR); } if (values == null) { throw new IllegalArgumentException(MISSING_VALUES); } // we expect key/value pairs passed one after another such as: // [key1 value1 key2 value2], etc // So we go through them with a 2 step loop int totalParsedArgs = values.length; for (int i = 0; i < totalParsedArgs; i += 2) { if (values[i] == null) { break; } String key = values[i]; String value = values[i + 1]; try { int x = Integer.parseInt(value); editor.putInt(key, x); } catch (NumberFormatException e) { try { float y = Float.parseFloat(value); editor.putFloat(key, y); } catch (NumberFormatException e1) { if (value.equals("true") || value.equals("false")) { editor.putBoolean(key, Boolean.parseBoolean(value)); } else { editor.putString(key, value); } } } } } }