1

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

  • register your receiver in onResume() and unregister it in onPause() method of your activity – Sandeep dhiman Jan 02 '20 at 12:46
  • @Sandeepdhiman , i tried and now still its not working but another line of warning occurred W/DownloadManager: Path appears to be invalid: /storage/emulated/0/Download/myfile.pdf – Tarandeepsingh Jan 02 '20 at 13:14
  • Btw, the link that was in the log should be redacted as opening the link on a browser leads to a PDF document of a certificate, which could hold information which may be potentially private/confidential. Either add [Firebase Storage security rules](https://firebase.google.com/docs/storage/security/start) or remove the link to the individual URL. – Edric Jan 02 '20 at 14:42
  • Additionally, other files can be found listed on the same Cloud Storage bucket you've provided in the log that contains even more personal info. **Please secure your Cloud Storage security rules.** – Edric Jan 02 '20 at 14:50
  • @Edric thanks for info i have edited that but it would be helpful if you could help me regarding the issue i am facing? – Tarandeepsingh Jan 02 '20 at 15:25
  • @Sandeepdhiman thanks , somehow when i tried to uninstall and reinstall, it started working – Tarandeepsingh Jan 03 '20 at 07:23

1 Answers1

1

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