【问题标题】:how to use broadcastReciever to notify Download Complete using Download Manager如何使用广播接收器通过下载管理器通知下载完成
【发布时间】:2020-04-21 03:04:33
【问题描述】:

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

我找到了这个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);
                }

记录sn-p

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

【问题讨论】:

  • 在 onResume() 中注册您的接收器并在您的活动的 onPause() 方法中取消注册
  • @Sandeepdhiman ,我试过了,现在它仍然无法正常工作,但出现另一行警告 W/DownloadManager:路径似乎无效:/storage/emulated/0/Download/myfile.pdf
  • 顺便说一句,日志中的链接应该被编辑,因为在浏览器上打开链接会导致证书的 PDF 文档,其中可能包含可能是私人/机密的信息。添加 Firebase Storage security rules 或删除指向单个 URL 的链接。
  • 此外,您在日志中提供的同一个 Cloud Storage 存储分区中列出了其他文件,其中包含更多个人信息。 请确保您的云存储安全规则。
  • @Edric 感谢我编辑的信息,但如果你能帮助我解决我面临的问题会有所帮助吗?

标签: android firebase broadcastreceiver firebase-storage android-download-manager


【解决方案1】:

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

感谢@Sandeepdhiman

在 onResume 函数中注册接收者并在 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);
    }

这似乎对我有用

【讨论】:

    猜你喜欢
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多