title: Register broadcast receiver in non-activity class
body: <p>I try to register and unregister broadcastreceiver in non-activity class. </p>
  
  <pre><code>public class AppDetectionEnabler implements OnCheckedChangeListener {
  protected final Context mContext;
  private Switch mSwitch;
  private String config;
  PackageManager pm;
  ComponentName receiver;
  private IntentFilter intentFilter;
  
  public AppDetectionEnabler(Context context, Switch swtch, String config) {
      mContext = context;
      this.config = config;
      receiver = new ComponentName(mContext, AppDetectionService.class);
      pm = mContext.getPackageManager();
      intentFilter = new IntentFilter();
      intentFilter.addAction("android.intent.action.PACKAGE_ADDED");
      intentFilter.addAction("android.intent.action.PACKAGE_REMOVED");
      setSwitch(swtch);
  }
  
   public void onCheckedChanged(CompoundButton view, boolean isChecked) {
      SharedPreferences prefs;
      Editor editor;
  
      prefs =  mContext.getSharedPreferences("prefs_app", Context.MODE_PRIVATE);
      editor = prefs.edit();
  
      if(config == "AppDetection"){
          if(!isChecked){
  
              mContext.unregisterReceiver(AppDetectionService.class);
          }
          else if(isChecked){
              mContext.registerReceiver(AppDetectionService.class, intentFilter);
  
          }
      }
  
      editor.putBoolean("APP_ENABLED", isChecked);
      editor.commit();
  
  }
  }
  </code></pre>
  
  <p>I did it like above in onCheckedChanged method. But it is not working. I create intenfilter in above class so, there is no receiver part in manifest.xml..</p>
  
  <p>when i look for error it says 'rename'.. Is there anything like 'Broadcastreceiver doesn't work in non-acticity class' or smtg like that?</p>
  
  <p><img src="https://i.stack.imgur.com/uLPzi.png" alt="enter image description here"></p>
  
  <p>AppDetectionService.java</p>
  
  <pre><code>public class AppDetectionService extends BroadcastReceiver{
  
  ArrayList&lt;String&gt; added = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; deleted = new ArrayList&lt;String&gt;();
  Bundle b=new Bundle();
  PackageManager pm;
  Dictionary&lt;String,PermissionInfo&gt; permissions;
  SharedPreferences prefs;
  Editor editor;
  Set&lt;String&gt; addSet = new HashSet&lt;String&gt;();
  
  @Override
  public void onReceive(Context context, Intent intent) {
  
      //gets the installed or removed application
      Uri data = intent.getData();
      Log.i(AppDetectionService.class.getName(), "Action: " + intent.getAction());
      Log.i(AppDetectionService.class.getName(), "The DATA: " + data);
      prefs = context.getSharedPreferences("added", Context.MODE_PRIVATE);
      editor = prefs.edit();
  
      if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){
          permissions = new Hashtable&lt;String,PermissionInfo&gt;();
  
          try{    
              pm = context.getPackageManager();
              String[] pName = data.toString().split(":");
              PackageInfo packageInfo = pm.getPackageInfo(pName[1], PackageManager.GET_PERMISSIONS);
  
              added.add(pName[1]);
              addSet.addAll(added);
              editor.putInt("added", added.size());
              editor.putStringSet("added", addSet);
  
              //permissions
              if (packageInfo.requestedPermissions != null){
                  for (String permName : packageInfo.requestedPermissions)
                  {
                      try
                      {
                          permissions.put(pName[1], pm.getPermissionInfo(permName, PackageManager.GET_META_DATA));
  
                      }
                      catch (NameNotFoundException e){}
                  }
              }
  
          }catch (NameNotFoundException e){}
  
          editor.commit();
      }
  
      if(intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")){
          permissions = new Hashtable&lt;String,PermissionInfo&gt;();
  
          pm = context.getPackageManager();
          String[] pName = data.toString().split(":");
          deleted.add(pName[1]);
  
      }
  
  
  
  
       }
  
  
  }
  </code></pre>