java - BroadcastReceiver 不起作用(使用 android 中的下载管理器)
问题描述
我想让我的 APP 监听下载管理器发送的下载完成操作,然后更改 VideoView 源以播放新下载的视频。
我的代码结构如下所示:
- MainActivity.java :运行 UI 线程,每 15 分钟启动 Job Scheduler 以检查是否有新视频可用。
- TriggerJobService.java :启动一个新线程来执行工作流,包括下载新视频、重置 VideoView。
我在 TriggerJobService.java 中初始化广播接收器
public class TriggerJobService extends JobService {
...
public BroadcastReceiver onDownloadComplete;
public int downloadFlag = 0;
public long downloadID;
...
private void downloadFile(String newName) throws JSONException {
Log.d(TAG, newName);
String link = "link/to/new/video" + newName;
Uri uri = Uri.parse(link);
String destName = "v" + newName;
DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDescription("Downloading");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(false);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, destName);
downloadID = downloadmanager.enqueue(request);
Log.d(TAG, String.valueOf(downloadID));
onDownloadComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Fetching the download id received with the broadcast
Log.d(TAG, "inside onDownloadComplete");
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
//Checking if the received broadcast is for our enqueued download by matching download id
if (downloadID == id) {
Log.d(TAG, "downloadFile: Successfully downloaded file.");
downloadFlag = 1;
}
}
};
}
...
private void mWorkflow(final JobParameters param) {
...
downloadFile(fileName);
if(downloadFlag == 1){
resetVideo(mVideoView, fileNameNew);
}else{
Log.d(TAG, "download failed");
}
...
}
我在 MainActivity.java 中注册广播接收器并每 15 分钟启动一次作业服务:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scheduleJob();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
TriggerJobService tjs = new TriggerJobService();
registerReceiver(tjs.onDownloadComplete,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
...
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void scheduleJob() {
ComponentName componentName = new ComponentName(this, TriggerJobService.class);
JobInfo info = new JobInfo.Builder(123, componentName)
.setRequiresCharging(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setPersisted(true)
.setPeriodic(15 * 60 * 1000)
.build();
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = scheduler.schedule(info);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d(TAG, "Job scheduled");
} else {
Log.d(TAG, "Job scheduling failed");
}
}
但我总是收到日志说“下载失败”,这表明接收器从未从下载管理器获得下载完成操作。
PS:我已经确保我的工作流程的其他部分运行良好。
我该如何解决?谢谢!!
解决方案
推荐阅读
- javascript - 如何从 redux 操作重定向到组件页面
- c# - 串行端口连接 - 仅适用于断点
- c++ - 用于缓存敏感矩阵乘法的多级平铺
- mysql - MySQL:在表插入后 15 分钟安排事件
- javascript - 使用 javascript 覆盖 chrome 默认快捷方式
- mysql - MySQL 日期范围查询索引仅适用于有限范围
- reactjs - redux/react-testing-library 的初始状态对于下一个测试用例保持不变
- mongodb - 如何保持 Azure blob 存储和 Mongo db 之间的一致性
- sql-server-2008 - 如何在 SSMS 中连接到 Azure SQL DB
- javascript - 仅匹配正则表达式的一部分