一、解决思路
通过监听ContentObserver监听下载路径content://downloads
二、具体步骤
1 设置监听器
context.getContentResolver()
.registerContentObserver(Uri.parse("content://downloads"), true, new ContentObserver() {
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
parseData(uri);
}
});
2 数据解析
- Uri过滤
private void parseData(Uri uri) {
List<String> segments = uri.getPathSegments();
if (segments != null && segments.size() > 1) {
String segment = (String) segments.get(1);
synchronized (this.pathSegments) {
if (this.pathSegments.contains(segment)) {
return;
}
this.pathSegments.add(segment);
}
obtainAppInfo(segment);
}
}
- 应用信息获取:
private void obtainAppInfo(String segment){
...
cursor = queryDownloadPkg(segment);
if (cursor == null || cursor.getColumnCount() < 15) {
return;
}
cursor.moveToNext();
String marketName = cursor.getString(9); // 应用市场包名
String appLink = cursor.getString(15); // 下载应用的包名链接
String appName = cursor.getString(20); //获取应用名称
...
}
private Cursor queryDownloadPkg(String segment) {
return this.context.getContentResolver()
.query(Uri.parse("content://downloads/public_downloads/" + segment),
(String[]) null, (String) null, (String[]) null, (String) null);
}