Pass data to BroadcastReceiver from Activity using DownloadManager

后端 未结 4 1592
小鲜肉
小鲜肉 2020-12-11 18:35

I am trying to pass an object to a BroadcastReceiver which will do something when a download is finished. How do I access the Intent object in the BroadcastReceiver\'s onRec

相关标签:
4条回答
  • 2020-12-11 19:12

    while calling send broadcast send data as

        Intent i = new Intent();
        i.putExtra("string_example", "here is a broadcasted string");
        i.putExtra("int_example", 100);
        sendBroadcast(i);
    

    and in side onReceive() get data from Intent as

     @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(getString(R.string.app_name), "broadcast string: " + intent.getStringExtra("string_example"));
            Log.d(getString(R.string.app_name), "extra!: " + intent.getIntExtra("int_example",0));
    
    
        }
    

    Modify as ur need...

    0 讨论(0)
  • 2020-12-11 19:22

    Before download is executed, save the value in SharedPreference

    editor.putInt(MainActivity.CERIS_LAST_DW_ID_KATALOG, m_intIdKatalog);
    editor.commit();
    

    Then in onReceive get the value from Shared Preference

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        SharedPreferences mCeris;
        mCeris = arg0.getSharedPreferences(MainActivity.CERIS_PREFERENCES,
                Context.MODE_PRIVATE);
    
        int m_intIdKatalog = mCeris.getInt(MainActivity.CERIS_LAST_DW_ID_KATALOG, 0);   
    }
    
    0 讨论(0)
  • 2020-12-11 19:22

    I was struggling with this because using local storage doesn't work if you're trying to keep track of multiple queued downloads. Luckily the brainiancs at Google pass the Download ID through as an extra, so you can use:

            long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0)
    

    Then use

                public class DownloadReceiver extends BroadcastReceiver
                {
                    @Override
                    public void onReceive(final Context context, final Intent intent)
                    {
                        long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0)
    
                        DownloadManager manager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
    
                        DownloadManager.Query query = new DownloadManager.Query();
                        query.setFilterById(downloadId);
    
                        Cursor cursor = manager.query(query);
    
                        if (cursor.moveToFirst()) {
                            int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                            int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
                        }
    
                        cursor.close();
    
                    }
                }
    
    0 讨论(0)
  • 2020-12-11 19:29

    But now I want to pass an object from my activity to the BroadcastReceiver.

    That's not possible. The BroadcastReceiver does not exist, except when receiving the broadcast. Your entire process is perhaps gone by the time the download is complete.

    You are welcome to store something in a persistent location (SharedPreferences, database, file) and read that in from onReceive(). That's the only way to pass data to an object that does not exist in a process that may not yet exist.

    0 讨论(0)
提交回复
热议问题