Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (C) 2014
 * Bearstouch Software : <mail@bearstouch.com>
 *
 * This file is part of Bearstouch Android Lib.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.content.Context;

import android.content.SharedPreferences;

import android.os.Environment;

import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class Main {
    public static boolean savePreferencesInExternal(Context ctx, SharedPreferences pref) {

        Properties propfile = new Properties();
        Map<String, ?> keymap = pref.getAll();
        Iterator<String> keyit = keymap.keySet().iterator();
        Log.d("External", "Saving prefrences to external Storages");

        while (keyit.hasNext()) {
            String key = keyit.next();
            propfile.put(key, keymap.get(key));
        }
        if (isExternalStorageAvailableforWriting() == true) {
            try {
                propfile.storeToXML(new FileOutputStream(new File(ctx.getExternalFilesDir(null), "install_info")),
                        null);
            } catch (Exception e) {

                e.printStackTrace();
            }

            Log.d("External", "Saved prefrences to external ");
            return true;
        } else {
            Log.d("External", "Failed to Save prefrences for external ");
            return false;
        }
    }

    public static boolean isExternalStorageAvailableforWriting() {

        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            return true;
        } else {
            return false;
        }

    }
}