title: register/unregister a service through the toggle button more than once
body: <p>This is what my code is... and I am getting a problem because of registering and unregistering(multiple times) my receiver(which is starting a service).</p>
  
  <p>the problem is that: i have seen that the 'receiver' variable becomes NULL after once executing registerReceiver and unregisterReceiver commands... specifically, after I register and unregister and then again register the receiver, the receiver has NULL only, and hence, while unregistering it again, it gives an error! so basically, my app is not able to register a Receiver again after unregistering it once. why is that a problem?</p>
  
  <pre><code>public class startScreen  extends Activity {
      /** Called when the activity is first created. */
  
      private BroadcastReceiver receiver=new BroadcastReceiver(){
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Intent serviceIntent = new Intent();
          serviceIntent.setAction("com.example.MyService");
          context.startService(serviceIntent);
  
      }
  
      };
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.initial);
  
         final IntentFilter filter = new IntentFilter();
          filter.addAction("android.net.wifi.STATE_CHANGE");
  
  
  
        Button button = (Button) findViewById(R.id.button1);
        final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
        try
      {
                   ... some code...             
        if(bool == true)
        {
          toggleButton.setChecked(true);
          this.registerReceiver(receiver, filter);
        }
        else
            toggleButton.setChecked(false);
      }catch(Exception e) {
             Log.e("Error", "Database", e);
            } finally {
                           ...
            }
  
        toggleButton.setOnClickListener(new OnClickListener() {        
            public void onClick(View v) {
                if((toggleButton.isChecked()))
                {
                    getBaseContext().registerReceiver(receiver, filter);
  
                }
                else
                {
                    if (receiver != null){
                        getBaseContext().unregisterReceiver(receiver);
                           receiver = null;
              }
  
                }
            }
      });
      }
  
      @Override
      protected void onResume(){
          super.onResume();
                if(bool == true)
                {
                 if(receiver == null)
                     this.registerReceiver(receiver, filter);
                }                       
      }
      @Override
      protected void onPause(){
          super.onPause();
      if (receiver != null){
          this.unregisterReceiver(receiver);
          receiver = null;
      }
      }
  }
  </code></pre>