0

After "observing" about ContentObserver I noticed that OnChange before API 16 not passing any parameters, so I decided to implement it to each row on my project (the project is downloading url's with the build-in DownloadManager)

for each row I'm creating a new ContentObserver :

    Uri downloadsContentUri = Uri.parse("content://downloads/my_downloads/downloads/" + appDownloadId);
    DownloadsObserver downloadsObserver = new DownloadsObserver(downloadId, new Handler());
    getContentResolver().registerContentObserver(downloadsContentUri, true, downloadsObserver);

as you can see the Uri always changing by the row id (its the same on the DownloadManager DB I've checked), and after this code I'm storing all the observer on a list, the strange thing is the OnChange is being called only once and only for the first download, and never called again even after it finished downloading..

what I'm doing wrong here? How can I accomplish observing each row on the DownloadManager DB? (I'm a system app)

thanks.

joseRo
  • 1,337
  • 3
  • 18
  • 35
  • Have your consider registering one ContentObserver to uri and inside the onChange method checking the last record which caused the change? – piobab Jan 02 '14 at 22:02
  • *after googeling it* How do I check the last record which caused the change? – joseRo Jan 03 '14 at 09:19
  • Are you using autoincrement on the _id? – piobab Jan 03 '14 at 09:45
  • the id is the id given to me by the Downloads.db of the DownloadManager, I opened this table (rooted device) and confirmed this is the id of the download I'm looking for... – joseRo Jan 03 '14 at 15:03
  • Do you have any field in the table which can identify the last record? Some timestamp? – piobab Jan 03 '14 at 15:05
  • I don't know if adding a timestamp will help because the writing to this table are very intense for each download.... is this really can be a solution? – joseRo Jan 05 '14 at 08:24
  • I don't see any other solution for api below 16. – piobab Jan 05 '14 at 09:27
  • After doing a quick look-over on the `DownloadProvider`, it looks like what you're attempting is feasible, as it does notify on the specific `Uri` with the id included. My guess is that you are not encoding the `Uri` properly. You can debug this by comparing the format of your encoded `Uri` with the `Uri` that is returned in the new `onChange()` method, and see if there are any obvious discrepancies. – corsair992 Jan 05 '14 at 14:56
  • corsair992 you are talking about API 16? because before 16 onChange doesn't have URI param....I need it to work also on API 15 – joseRo Jan 05 '14 at 15:06
  • Yes, I am only talking about debugging the `Uri` format from API 16. – corsair992 Jan 05 '14 at 15:23

1 Answers1

1

I got it!

This is my solution for ContentObserver for each row, this can be applied on API 15 (15 doesn't have OnChange with URI param like on API 16 ). Of course you need a new ContentObserver for each row.

the mistake was at this line

 Uri downloadsContentUri = Uri.parse("content://downloads/my_downloads/downloads/" + appDownloadId);

it should be without the "/downloads/", like this:

Uri downloadsContentUri = Uri.parse("content://downloads/my_downloads/" + appDownloadId);

and this is my ContentObserver:

public class DownloadsObserver extends ContentObserver
{
private long mAppDownloadId = 0;

public DownloadsObserver(Handler handler)
{
    super(handler);
}

public DownloadsObserver(long appDownloadId, Handler handler)
{
    super(handler);
    mAppDownloadId = appDownloadId;
}


@Override
public void onChange(boolean selfChange)
{
    super.onChange(selfChange);
    // Do some work on this row with the id

}
}
joseRo
  • 1,337
  • 3
  • 18
  • 35