title: How can I pass a list of selected packages to a service in Android?
body: <p>I am trying to setup a whitelist in my app. I have a service setup that will trigger another service when it detects that a certain app had started. I have a package picking activity that builds a nice list with checkboxes. When I select a checkbox, I want to add that selected app to the "whitelist". I then want my service to check against that whitelist while it runs. I cannot get my whitelist to pass its list over to my service. Here is the package list I am using...</p>
  
  <pre><code>    @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      getActionBar().setDisplayHomeAsUpEnabled(true);
  
      // this has to be done before any preferences are populated!
      getPreferenceManager().setSharedPreferencesMode(Context.MODE_WORLD_READABLE);
  
      addPreferencesFromResource(R.xml.preferences2);
  
      // get a list of launchable apps
      PackageManager pm = getPackageManager();
      Intent main = new Intent(Intent.ACTION_MAIN, null);
  
      main.addCategory(Intent.CATEGORY_LAUNCHER);
  
      List&lt;ResolveInfo&gt; launchables = pm.queryIntentActivities(main, 0);
  
      Collections.sort(launchables,
              new ResolveInfo.DisplayNameComparator(pm));
  
      PreferenceCategory targetCategory = (PreferenceCategory) findPreference("targetCategory");
      targetCategory.setTitle(getString(R.string.settings_header));
  
      for (ResolveInfo info : launchables) {
  
          // get app info
          ActivityInfo activity = info.activityInfo;
          ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
          final String pkgName = name.getPackageName();
          CharSequence appName = info.loadLabel(pm);
  
          CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
  
          // set the data
          checkBoxPreference.setKey(pkgName);
          checkBoxPreference.setTitle(appName);
          checkBoxPreference.setSummary(pkgName);
          checkBoxPreference.setIcon(info.loadIcon(pm));
  
          // add to category
          targetCategory.addPreference(checkBoxPreference);
      }
  }
  
  protected void onResume() {
      super.onResume();
      getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  }
  
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
      Preference pref = findPreference(key);
      CheckBoxPreference cbtp = (CheckBoxPreference) pref;
      final String pkgName = key;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the service that I am using to check for the running app. I need to check if the running app has been selected from that list. If the current app has been checked, I want my service to run. I have the service running fine, and the list builds great. I just cant seem to get them to work together.</p>
  
  <pre><code>                @Override
              public void run() {
                  SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
                   vibenabled = mySharedPreferences.getBoolean("vib_preference", false);
                   pkgName = prefs.getBoolean(pkgName, false);
                  ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
                  Log.d(TAG, "Its running");   
  
          String lastPkg = am.getRunningTasks(1).get(0).topActivity.getPackageName();
         if (selectedPkg.equals(lastPkg)) {
                startService();
  </code></pre>