load SharedPreferences From File - Android Android OS

Android examples for Android OS:SharedPreferences

Description

load SharedPreferences From File

Demo Code

/*******************************************************************************
 * Copyright (c) 2014 Hoang Nguyen.// w w w. ja v  a 2s. co  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Hoang Nguyen - initial API and implementation
 ******************************************************************************/
//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.ObjectInputStream;

import java.util.Map;
import java.util.Map.Entry;

import java.util.zip.GZIPInputStream;
import android.content.Context;

import android.content.SharedPreferences.Editor;

import android.preference.PreferenceManager;

public class Main {
    @SuppressWarnings("unchecked")
    static boolean loadSharedPreferencesFromFile(Context context, File src) {
        boolean res = false;
        ObjectInputStream input = null;
        try {
            GZIPInputStream inputGZIP = new GZIPInputStream(
                    new FileInputStream(src));
            input = new ObjectInputStream(inputGZIP);
            Editor prefEdit = PreferenceManager
                    .getDefaultSharedPreferences(context).edit();
            prefEdit.clear();
            Map<String, Object> entries = (Map<String, Object>) input
                    .readObject();
            for (Entry<String, ?> entry : entries.entrySet()) {
                Object v = entry.getValue();
                String key = entry.getKey();

                if (v instanceof Boolean)
                    prefEdit.putBoolean(key, ((Boolean) v).booleanValue());
                else if (v instanceof Float)
                    prefEdit.putFloat(key, ((Float) v).floatValue());
                else if (v instanceof Integer)
                    prefEdit.putInt(key, ((Integer) v).intValue());
                else if (v instanceof Long)
                    prefEdit.putLong(key, ((Long) v).longValue());
                else if (v instanceof String)
                    prefEdit.putString(key, ((String) v));
            }
            prefEdit.commit();
            res = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return res;
    }
}

Related Tutorials