【问题标题】:How To Have Notification When Download Complete下载完成后如何通知
【发布时间】:2021-08-31 14:26:52
【问题描述】:

我的 Android 应用中有下载功能。当我下载文件时,它会通过 toast 通知我。我想知道如何在下载完成后收到 toast 通知。我尝试了其他建议,但没有成功找到我预期的结果。

这是下载代码。

webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition,
                                        String mimeType, long contentLength) {
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                request.setMimeType(mimeType);
                String cookies = CookieManager.getInstance().getCookie(url);
                request.addRequestHeader("cookie",cookies);
                request.addRequestHeader("User-Agent",userAgent);
                request.setDescription("Downloading File");
                request.setTitle(URLUtil.guessFileName(url,contentDisposition,mimeType));
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                        URLUtil.guessFileName(url, contentDisposition, mimeType));
                DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                downloadManager.enqueue(request);
                Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_SHORT).show();
            }
        });

提前谢谢你。

【问题讨论】:

    标签: java android android-studio android-download-manager download-manager


    【解决方案1】:

    您可以创建一个Broadcast Receiver 来处理任何已完成下载的文件:

    主要活动

    public class MainActivity extends AppCompatActivity {
    
    // create an object
    OnComplete onComplete ;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        WebView webView = null;
        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition,
                                        String mimeType, long contentLength) {
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                request.setMimeType(mimeType);
                String cookies = CookieManager.getInstance().getCookie(url);
                request.addRequestHeader("cookie",cookies);
                request.addRequestHeader("User-Agent",userAgent);
                request.setDescription("Downloading File");
                request.setTitle(URLUtil.guessFileName(url,contentDisposition,mimeType));
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                        URLUtil.guessFileName(url, contentDisposition, mimeType));
                DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                downloadManager.enqueue(request);
                Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_SHORT).show();
            }
        });
    
        // the broadcast receiver object
        onComplete = new OnComplete();
    
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // to start handling any download that completes
        registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // to stop handling download complete when exit app
        unregisterReceiver(onComplete);
    }
    
    }
    

    广播接收器(OnComplete)

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class OnComplete extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // these codes will run when download completed
        // And you can do some thing
    }
    }
    

    请不要忘记在Mainfest.xml<application/> 标签中添加此内容

    <receiver android:name=".OnComplete" />
    

    【讨论】:

    • 我这里还有一个问题 (stackoverflow.com/questions/69002074/…)。你能帮忙吗?
    • 您意识到在 onReceive() 中可以获取下载文件的 uri 吗?您可以在 ACTION_VIEW 意图中使用该 uri,让用户选择的 pdf 阅读器打开您的文件。
    • @blackapps 是的,先生,您是对的,这将是对应用程序的一个很好的补充
    • @blackapps 请你详细解释一下如何做到这一点?我真的不知道该怎么做。
    • 这是我在 Receive 中所做的 -> @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context.getApplicationContext(), "Download Success", Toast.LENGTH_SHORT) 。显示(); } }
    【解决方案2】:

    感谢@a7d.24_ 的回答。

    我已经简化了答案,所以我不需要创建新的 Java 类 (OnComplete),因此不需要将它添加到 Manifest.XML。

    我只需在 webView.setDownloadListener 中添加一行代码并在 WebView 类中创建新的 BroadcastReceiver。

     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    
     ...
    
            webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition,
                                        String mimeType, long contentLength) {
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                request.setMimeType(mimeType);
                String cookies = CookieManager.getInstance().getCookie(url);
                request.addRequestHeader("cookie",cookies);
                request.addRequestHeader("User-Agent",userAgent);
                request.setDescription("Downloading File");
                request.setTitle(URLUtil.guessFileName(url,contentDisposition,mimeType));
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                        URLUtil.guessFileName(url, contentDisposition, mimeType));
                DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                //Registering receiver in Download Manager
                registerReceiver(onCompleted, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));    
                downloadManager.enqueue(request);
                Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_SHORT).show();
            }
        });
    
     ...
    
     BroadcastReceiver onCompleted = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context.getApplicationContext(), "Download Finish", Toast.LENGTH_SHORT).show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多