title: Unable to register BroadcastReceiver
body: <p>I am unable to register my BroadcastReceiver. Whenever I try to execute my code its showing "Unable to register receiver" in logcat.</p>
  
  <p>Here is my code where I am registering my receiver:</p>
  
  <pre><code>public class Preferences extends PreferenceActivity {
      private Button button;
      ListPreference lp;
      private Context context;
      private long duration;
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          addPreferencesFromResource(R.xml.preferences);
          context = this;
          lp = (ListPreference) findPreference("autoduration");
          final Intent intent = new Intent(context, BackService.class);
          lp.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
              public boolean onPreferenceChange(Preference preference, Object newValue) {
                  String newSelection = newValue.toString();
                  int index = lp.findIndexOfValue(newSelection);
                  stopService(intent);
                  if (index == 0) {
                      duration = 15000;
                  } else if (index == 1) {
                     duration = 12000;
                  } else if (index == 2) {
                      duration = 10000;
                  } else if (index == 3) {
                      IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
                      filter.addAction(Intent.ACTION_SCREEN_OFF);
                      BroadcastReceiver receiver = new ScreenReceiver();
                      registerReceiver(receiver, filter);
                  }
                  intent.putExtra("duration", duration);
                  startService(intent);
                  return true;
              }
          });
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          unregisterReceiver(receiver);
      }
  }
  </code></pre>
  
  <p>and this is my receiver:</p>
  
  <pre><code>public class ScreenReceiver extends BroadcastReceiver {
      public static boolean screenOn = true;
      private static final String TAG = "ScreenReceiver";
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
               screenOn = true;
               Log.e(TAG, "I am receiver");
          } else {
              screenOn = false;
          }
      }
  }
  </code></pre>