gpt4 book ai didi

java - 在下载管理器中,如何从通知栏获取其 "cancel"状态?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:49 25 4
gpt4 key购买 nike

我正在使用下载管理器在 android 中下载文件。但是在点击通知栏中的“取消”按钮的情况下,我无法收到任何广播。

我只找到两个广播:

1.DownloadManager.ACTION_DOWNLOAD_COMPLETE2.下载管理器.ACTION_NOTIFICTION_CLICKED

下载管理器取消时是否有任何广播?如果没有那么请给我任何解决方案如何处理它?<​​/p>

最佳答案

DownloadManager 在处理用户下载任务时会将信息写入数据库。所以我们可以检查数据库的状态来了解任务是否被取消。

1。使用 DownloadManager 的 api,定期轮询状态

将下载任务加入队列后,启动以下线程进行检查。

private static class DownloadQueryThread extends Thread {

private static final String TAG = "DownloadQueryThread";
private final WeakReference<Context> context;
private DownloadManager downloadManager;
private final DownloadManager.Query downloadQuery;
private boolean shouldStopQuery = false;
private boolean downloadComplete = false;
private static final Object LOCK = new Object();
private final BroadcastReceiver downloadCompleteBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
synchronized (LOCK) {
shouldStopQuery = true;
downloadComplete = true;
}
}
}
};

/**
* Create from context and download id
* @param context the application context
* @param queryId the download id from {@link DownloadManager#enqueue(DownloadManager.Request)}
*/
public DownloadQueryThread(Context context, long queryId) {
this.context = new WeakReference<>(context);
this.downloadQuery = new DownloadManager.Query().setFilterById(queryId);
this.downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
}

@Override
public void run() {
super.run();
if (context.get() != null) {
context.get().registerReceiver(downloadCompleteBroadcastReceiver,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

while (true) {

if (downloadManager != null) {
Cursor cursor = downloadManager.query(downloadQuery);
if (cursor != null && cursor.moveToFirst()) {
Log.d(TAG, "download running");
} else {
shouldStopQuery = true;
}
}

synchronized (LOCK) {
if (shouldStopQuery) {
if (downloadComplete) {
Log.d(TAG, "download complete");
} else {
Log.w(TAG, "download cancel");
}
break;
}
}

try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

if (context.get() != null) {
context.get().unregisterReceiver(downloadCompleteBroadcastReceiver);
}
}
}

2.使用ContentObserver在数据库改变时得到通知

下载管理器的内容uri应该是content://downloads/my_downloads,我们可以监控这个数据库的变化。当您使用下载 ID 开始下载时,将创建一行 content://downloads/my_downloads/{downloadId}。我们可以检查此游标以了解此任务是否已取消。如果返回的游标为空或null,表示在数据库中没有找到记录,则用户取消本次下载任务。

// get the download id from DownloadManager#enqueue
getContentResolver().registerContentObserver(Uri.parse("content://downloads/my_downloads"),
true, new ContentObserver(null) {
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
if (uri.toString().matches(".*\\d+$")) {
long changedId = Long.parseLong(uri.getLastPathSegment());
if (changedId == downloadId[0]) {
Log.d(TAG, "onChange: " + uri.toString() + " " + changedId + " " + downloadId[0]);
Cursor cursor = null;
try {
cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
Log.d(TAG, "onChange: running");
} else {
Log.w(TAG, "onChange: cancel");
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
}
});

关于java - 在下载管理器中,如何从通知栏获取其 "cancel"状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48248300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com