Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.net.Uri;

import android.support.annotation.Nullable;

import android.content.*;
import android.preference.*;

public class Main {
    public static Context applicationContext;

    /**
     * Set a shared preference for an Uri.
     *
     * @param preferenceId the id of the shared preference.
     * @param uri          the target value of the preference.
     */
    public static void setSharedPreferenceUri(final int preferenceId, @Nullable final Uri uri) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        if (uri == null) {
            editor.putString(applicationContext.getString(preferenceId), null);
        } else {
            editor.putString(applicationContext.getString(preferenceId), uri.toString());
        }
        editor.apply();
    }

    /**
     * Retrieve the default shared preferences of the application.
     *
     * @return the default shared preferences.
     */
    private static SharedPreferences getSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(applicationContext);
    }
}