title: Receiving Event, if a file was downloaded/ added to download folder
body: <p>I would like to receive an event whenever a file was added to a specific folder, e.g. the download folder. To reach this I tried 3 different approaches without any success. The target devices are Android 15+. Do you have any experience with any of these 3 approaches and could help out with a working sample?</p>
  
  <p><strong>APPROACH 1 - FileObserver:</strong></p>
  
  <p>In a background service I add a recursive file observer for the top folder as described <a href="https://github.com/owncloud/android/blob/master/src/com/owncloud/android/utils/RecursiveFileObserver.java" rel="nofollow noreferrer">here</a>. On Android 4/5 its working but on Android 6 no events are fired (<a href="https://code.google.com/p/android/issues/detail?id=189231" rel="nofollow noreferrer">known issue)</a> On top of that it seems that on Android 4/5 the file observer is not reliable. At some point the stopWatching() method gets called and from then on no event will be received. </p>
  
  <p>in onStartCommand(..) of the service: </p>
  
  <pre><code>    new MyFileObserver(Constants.DOWNLOAD_PATH, true).startWatching();
  </code></pre>
  
  <p><strong>APPROACH 2 - Content Observer:</strong></p>
  
  <p>I tried to tweak the content observer for my use case (as described <a href="https://stackoverflow.com/questions/20891705/contentobserver-per-row-implementation">here</a>), but I never receive any events. </p>
  
  <p>in onStart of the service: </p>
  
  <pre><code> getContentResolver().registerContentObserver( Uri.parse("content://download/"), true,
              new MyObserver(myHandler));
  </code></pre>
  
  <p>.</p>
  
  <pre><code>public class MyObserver extends ContentObserver {
    // left blank below constructor for this Contact observer example to work
    // or if you want to make this work using Handler then change below registering  //line
    public MyObserver(Handler handler) {
      super(handler);
    }
  
    @Override
    public void onChange(boolean selfChange) {
      this.onChange(selfChange, null);
      Log.e("", "~~~~~~ on change" + selfChange);
      // Override this method to listen to any changes
    }
  
    @Override
    public void onChange(boolean selfChange, Uri uri) {
      // depending on the handler you might be on the UI
      // thread, so be cautious!
      Log.e("", "~~~~~~ on change uri" + selfChange);
    }
  }
  </code></pre>
  
  <p><strong>APPROACH 3 - BroadcastReceiver:</strong></p>
  
  <p>With a BroadcastReceiver I try to get the ON_DOWNLOAD_COMPLETE_EVENT (as described <a href="https://stackoverflow.com/questions/18789246/broadcastreceiver-not-receiving-download-complete-action">here</a>. But nothing happens. </p>
  
  <p>in on StartCommand(...) of the service:</p>
  
  <pre><code> registerReceiver(new DownloadListenerService(), new IntentFilter(
              DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  </code></pre>
  
  <p>DownloadListenerService: </p>
  
  <pre><code>public class DownloadListenerService extends BroadcastReceiver {
      @Override
      public void onReceive(final Context context, Intent intent) {
          System.out.println("got here");
          SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
          SharedPreferences.Editor editor = settings.edit();
  
          String action = intent.getAction();
          if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
              String downloadPath = intent.getStringExtra(DownloadManager.COLUMN_URI);
              editor.putString("downloadPath", downloadPath);
              editor.commit();
          }
      }
  }
  </code></pre>
  
  <p>Manifest:</p>
  
  <pre><code>  &lt;receiver
              android:name=".DownloadListenerService"
              android:icon="@mipmap/ic_launcher"
              android:exported="true"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.DOWNLOAD_COMPLETE" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  </code></pre>