问题
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.
回答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
}
}
来源:https://stackoverflow.com/questions/20891705/contentobserver-per-row-implementation