首页 > 解决方案 > BroadcastReceiver 不起作用(使用 android 中的下载管理器)

问题描述

我想让我的 APP 监听下载管理器发送的下载完成操作,然后更改 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:我已经确保我的工作流程的其他部分运行良好。

我该如何解决?谢谢!!

标签: javaandroidbroadcastreceiverandroid-download-manager

解决方案


推荐阅读