如何使用 broadcastReciever 通知下载完成使用下载管理器

how to use broadcastReciever to notify Download Complete using Download Manager

我正在使用下载管理器下载文件,我想在下载完成后通知用户。

我找到了这个 answer 并尝试使用 BroadcastReciever 但出于某种原因,我不知道为什么接收器不工作。

代码如下:

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();
                }
            }
        };

正在下载代码片段

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);
                }

日志片段

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

清单文件

<?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>

取消注册接收者

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

pdf 正在正确下载(从日志中编辑)并显示在通知栏中,但我想在下载完成后做一些工作

我正在回答我的问题,以便如果其他人遇到同样的问题可以得到解决

感谢@Sandeepdhiman

在 onResume 函数中注册 reciever 并在 On pause 函数中注销似乎可行 我不知道为什么当我在 onCreate 中注册并在 onDestroy

中注销时它不起作用
@Override
    protected void onResume() {
        super.onResume();
        registerReceiver(broadcastReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

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

这似乎对我有用