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");
        }
        ...
    }

我注册广播接收器,并每隔15分钟在MainActivity.java中启动作业服务:

 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");
        }
    }

但是我总会收到一条日志,上面写着“下载失败”,这表明接收者从未从下载管理器中获得下载完成操作。

P.S .:我确保工作流程的其他部分都能正常工作。

我该如何解决?谢谢!!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...