Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.BroadcastReceiver;
import android.content.Context;

import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;

public class Main {
    /**
     * Register a {@link BroadcastReceiver} with the {@link LocalBroadcastManager}.
     *
     * @param context       used to get the instance of the {@link LocalBroadcastManager}.
     * @param receiver      to register with the {@link LocalBroadcastManager}.
     * @param intentFilters that the {@link BroadcastReceiver} is 'listening' for.
     */
    public static void registerReceiver(@NonNull Context context, @NonNull BroadcastReceiver receiver,
            @NonNull final String... intentFilters) {
        LocalBroadcastManager.getInstance(context).registerReceiver(receiver, createIntentFilter(intentFilters));
    }

    private static IntentFilter createIntentFilter(String[] intentFilters) {
        IntentFilter filter = new IntentFilter(intentFilters[0]);

        for (int i = 0; i < intentFilters.length; i++) {
            if (i == 0) {
                continue;
            }
            filter.addAction(intentFilters[i]);
        }

        return filter;
    }
}