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;

public class Main {
    private final static String KEY_BOOTSERVICES = "com.red_folder.phonegap.plugin.backgroundservice.BootServices";

    protected static void addBootService(Context context, String serviceName) {
        String serviceList = getBootServicesString(context);

        if (serviceList.length() == 0)
            serviceList = serviceName;
        else if (serviceList.contains(serviceName)) {
            // Already in the list, so ignore the request
        } else {
            serviceList += ";" + serviceName;
        }

        saveBootServices(context, serviceList);
    }

    private static String getBootServicesString(Context context) {
        SharedPreferences sharedPrefs = getSharedPreferences(context);

        return sharedPrefs.getString(KEY_BOOTSERVICES, "");
    }

    private static void saveBootServices(Context context, String serviceList) {
        SharedPreferences.Editor editor = getEditor(context);
        editor.putString(KEY_BOOTSERVICES, serviceList);
        editor.commit(); // Very important
    }

    private static SharedPreferences getSharedPreferences(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    private static SharedPreferences.Editor getEditor(Context context) {
        SharedPreferences sharedPrefs = getSharedPreferences(context);
        return sharedPrefs.edit();
    }
}