Before triggering broadcast check if there is any registered receivers - Android android.content.pm

Android examples for android.content.pm:PackageManager

Description

Before triggering broadcast check if there is any registered receivers

Demo Code

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class Main {

  /**/*w w w  . jav  a  2 s .c  o m*/
   * Before triggering broadcast check if there is any registered
   * receivers
   * 
   * @param context
   *          Current Activity context
   * @param intent
   *          Intent to be called
   */
  public static boolean isBroadcastSafe(Context context, Intent intent) {
    if (context == null)
      return false;
    PackageManager packageMgr = context.getPackageManager();
    List<ResolveInfo> receivers = packageMgr.queryBroadcastReceivers(intent, 0);
    return (receivers.size() > 0);
  }

}

Related Tutorials