Search code examples
androidfirebasebroadcastreceiverfirebase-storageandroid-download-manager

how to use broadcastReciever to notify Download Complete using Download Manager


I am downloading files using the download manager, and I want to notify the user after the download is completed.

I found this answer and tried using BroadcastReciever but for some reason, I don't know why the receiver is not working.

Here's the code:

BroadcastReceiver broadcastReceiver;
DownloadManager downloadmanager;
long iid;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_pdf);

        downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        registerReceiver(broadcastReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if(iid == intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1)){
                    Log.i("inside","onRecieve");
                    Toast.makeText(ShowPdfActivity.this, "Downloaded", Toast.LENGTH_SHORT).show();
                }
            }
        };

Downloading code Snippet

if(ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.ACCESS_NETWORK_STATE)==PackageManager.PERMISSION_GRANTED){
                    Log.i("permission ","already have");
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                    request.setTitle(name);
                    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                            DownloadManager.Request.NETWORK_MOBILE);
                   request.setAllowedOverRoaming(false);
                    request.setDescription("Downloading");
                    request.allowScanningByMediaScanner();    
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);                  request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,uploadPDF.getName());
                    request.setMimeType(".pdf");
                    iid = downloadmanager.enqueue(request);
                }

log snippet

2020-01-02 14:22:08.741 4540-4540/com.tarandeepsingh.inventory I/url: https//:sanplepdf.com
2020-01-02 14:22:08.743 4540-4540/com.tarandeepsingh.inventory I/permission: already have

manifest file

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tarandeepsingh.inventory">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ShowPdfActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

unregistering reciever

@Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }

pdf is downloading properly(redacted from Log) and showing in the notification bar , but i want to do some work when download is completed


Solution

  • i am answering my question so that if anyone else is facing same issue can get it solved

    thanks to @Sandeepdhiman

    registering reciever inside onResume function and unregistering it in On pause function seems to work i don't know why it was not working when i registered in onCreate and unregistered in onDestroy

    @Override
        protected void onResume() {
            super.onResume();
            registerReceiver(broadcastReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            unregisterReceiver(broadcastReceiver);
        }
    

    this seems to work for me