title: how to use android broadcast receiver
body: <p>Here below is an example of service, activity and broadcast receiver. An activity is a setting that make changes to a service. The broadcast receiver listens to changes in settings and update service. learner2learner</p>
  
  <pre><code>public class xService extends Service {
  
      @Override
      public void onCreate() {
          super.onCreate();
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return mBinder;
      }
  
      public static void setEnableNotification(int command) {
          Log.i("EnableNotification","Updated");
          if (command == 0)
              enableNotification = true;
          ...
      }
  }
  </code></pre>
  
  <p>The method below is a part of an activity that sends broadcast:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      ....
      IntentFilter filter = new IntentFilter();
          filter.addAction(NotifyServiceReceiver.ACTION);       
      registerReceiver(receiver,filter);
  }   
  final String ACTION="broadcast_settings";
  public void onClick(View view) {
      Intent intent = new Intent(ACTION);
  
      switch (view.getId()) {
      case R.id.switchNotification:
          if(switchNotification.isChecked()==true)
          {       
              intent.putExtra("EnableNotification", 0);
              Log.i("Security365","Notification is enabled.");
          }
          else if ....
          sendBroadcast(intent);
          break;
      }
  </code></pre>
  
  <p>The part below is my broadcast receiver:</p>
  
  <pre><code>public class xReceiver extends BroadcastReceiver {
  
      final String ACTION="broadcast_settings";
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if(intent.getAction().equals(ACTION)){
  
              int enableNotification = intent.getIntExtra("EnableNotification", 0);
  
              if (enableNotification == 0)
                  Serurity365Service.setEnableNotification(0);
          ...
          }
      }
  }
  </code></pre>
  
  <p>Mainfest</p>
  
  <pre><code>    &lt;receiver android:name=".xReceiver" android:enabled="true"&gt;&lt;/receiver&gt;
  </code></pre>