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

问题描述 投票:0回答:1

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

我找到了此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正在正确下载(从日志中删除)并显示在通知栏中,但我想在下载完成后做一些工作

android firebase broadcastreceiver firebase-storage android-download-manager
1个回答
0
投票

我正在回答我的问题,以便任何其他面临相同问题的人都可以解决

感谢@Sandeepdhiman

在onResume函数中注册接收器,然后在On暂停功能中注销它似乎可以正常工作我不知道为什么在我在onCreate中注册并在onDestroy中未注册时它不起作用

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

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

这似乎对我有用

© www.soinside.com 2019 - 2024. All rights reserved.