简体   繁体   English

使用LocalBroadcastManager注册BroadcastReceiver时,不会调用onReceive

[英]onReceive not being called when BroadcastReceiver is registered using LocalBroadcastManager

I've created a BroadcastReceiver to receive ACTION_DOWNLOAD_COMPLETE when my app starts downloading something using DownloadManager . 当我的应用程序开始使用DownloadManager时,我创建了一个BroadcastReceiver来接收ACTION_DOWNLOAD_COMPLETE As I want to capture ACTION_DOWNLOAD_COMPLETE only when downloading is started from my app, I've used LocalBroadcastManager . 因为我只想在从我的应用程序开始下载时捕获ACTION_DOWNLOAD_COMPLETE ,所以我使用了LocalBroadcastManager

But onReceiver is not being called at all. onReceiver根本没有被调用。 DownloadManager app shows that download is complete but onReceive is not triggered. DownloadManager应用程序显示下载已完成但未触发onReceive When I use registerReceiver it works as expected. 当我使用registerReceiver它按预期工作。 But this would let app being notified even if downloading is started by some other app. 但即使下载是由其他应用程序启动的,这也会让应用程序得到通知。 So LocalBroadcastManager is desired. 所以需要LocalBroadcastManager。

MainActivity 主要活动

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        downloadReceiver = new DownloadReceiver();

        LocalBroadcastManager.getInstance(this).registerReceiver(downloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));           

        downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        if(FileHelper.isStorageAvailable()) {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://example.com/image.jpg"));
            downloadManager.enqueue(request);
        }
    }
    @Override
    protected void onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(downloadReceiver);
        super.onPause();
    }

DownloadReciever DownloadReciever

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {

            long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
            downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(downloadId);
            Cursor c = downloadManager.query(query);

            if (c.moveToFirst()) {
                int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {                        
                    String title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));                 
                    Toast.makeText(context, title, Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

It simply don't call onRecieve as it should. 它根本不应该调用onRecieve Point out if I'm doing something wrong here. 如果我在这里做错了,请指出。 Been stuck here for quite time now. 被困在这里很长一段时间了。 I can't use registerReceiver as I need to track download complete action only if my app starts downloading. 我不能使用registerReceiver因为我只有在我的应用程序开始下载时才需要跟踪下载完成操作。

As I want to capture ACTION_DOWNLOAD_COMPLETE only when downloading is started from my app, I've used LocalBroadcastManager. 因为我只想在从我的应用程序开始下载时捕获ACTION_DOWNLOAD_COMPLETE,所以我使用了LocalBroadcastManager。

That is not going to work. 那不行。 DownloadManager does the download in a separate process, and it will use a system broadcast. DownloadManager在单独的过程中进行下载,它将使用系统广播。 The only broadcasts that you can receive via LocalBraodcastManager are the ones that you broadcast via LocalBroadcastManager . 您可以通过收到的唯一的广播LocalBraodcastManager通过广播的那些LocalBroadcastManager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BroadcastReceiver onReceive()在动态注册时不调用 - BroadcastReceiver onReceive() not Called when registered dynamically BroadcastReceiver - onReceive未被调用 - BroadcastReceiver - onReceive Not Being Called 注册时触发BroadcastReceiver onReceive - BroadcastReceiver onReceive triggered when registered 注册BroadcastReceiver后,OnReceive不会被调用 - OnReceive is not called even after BroadcastReceiver is registered 成功注册了一个BroadcastReceiver到requestActivityTransitionUpdates,但是onReceive从来没有调用过 - Successfully registered a BroadcastReceiver to requestActivityTransitionUpdates, but onReceive never called 不调用LocalBroadcastManager onReceive() - LocalBroadcastManager onReceive() not called Android,BroadCastReceiver中的onReceive被多次调用 - Android, onReceive in BroadCastReceiver is being called multiple times 没有调用BroadcastReceiver的onReceive方法 - BroadcastReceiver's onReceive method is not being called LocalBroadcastManager.sendBroadcast不触发onReceive上的BroadcastReceiver - LocalBroadcastManager.sendBroadcast not triggering BroadcastReceiver onReceive 在动态注册广播接收器的onReceive方法时,它总是被调用吗? - Is a BroadcastReceiver's onReceive method always called the moment it gets registered dynamically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM