Register an Intent BroadcastReceiver with the LocalBroadcastManager - Android Intent

Android examples for Intent:Broadcast

Description

Register an Intent BroadcastReceiver with the LocalBroadcastManager

Demo Code


//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 {
    /**//from w  w  w.  java  2  s .c o  m
     * 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;
    }
}

Related Tutorials