您的位置:首页 > 移动开发 > Android开发

立即获得下载任务完成的消息通知。Android DownloadManager通过注册一个广播监听系统的广播事件完成此操作,在创建广播时候,需要指明过滤器为:DownloadManager.ACTIO

2016-08-16 17:28 941 查看
立即获得下载任务完成的消息通知。Android DownloadManager通过注册一个广播监听系统的广播事件完成此操作,在创建广播时候,需要指明过滤器为:DownloadManager.ACTION_DOWNLOAD_COMPLETE

测试的主Activity MainActivity.Java

[java] view
plain copy

package zhangphil.demo;  

  

import android.app.Activity;  

import android.app.DownloadManager;  

import android.app.DownloadManager.Request;  

import android.content.BroadcastReceiver;  

import android.content.Context;  

import android.content.Intent;  

import android.content.IntentFilter;  

import android.net.Uri;  

import android.os.Bundle;  

import android.os.Environment;  

import android.util.Log;  

import android.widget.Toast;  

  

public class MainActivity extends Activity {  

  

    private BroadcastReceiver broadcastReceiver;  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        // setContentView(R.layout.activity_main);  

  

        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);  

  

        // 假设从这一个链接下载一个大文件。  

        Request request = new Request(  

                Uri.parse("http://apkc.mumayi.com/2015/03/06/92/927937/xingxiangyi_V3.1.3_mumayi_00169.apk"));  

  

        // 仅允许在WIFI连接情况下下载  

        request.setAllowedNetworkTypes(Request.NETWORK_WIFI);  

  

        // 通知栏中将出现的内容  

        request.setTitle("我的下载");  

        request.setDescription("下载一个大文件");  

        // 下载过程和下载完成后通知栏有通知消息。  

        request.setNotificationVisibility(Request.VISIBILITY_VISIBLE | Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);  

  

        // 此处可以由开发者自己指定一个文件存放下载文件。  

        // 如果不指定则Android将使用系统默认的  

        // request.setDestinationUri(Uri.fromFile(new File("")));  

  

        // 默认的Android系统下载存储目录  

        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "my.apk");  

  

        // enqueue 开始启动下载...  

        long Id = downloadManager.enqueue(request);  

        Log.d(this.getClass().getName(), "开始下载任务:" + Id + " ...");  

  

        listener(Id);  

    }  

  

    private void listener(final long Id) {  

  

        // 注册广播监听系统的下载完成事件。  

        IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);  

        broadcastReceiver = new BroadcastReceiver() {  

            @Override  

            public void onReceive(Context context, Intent intent) {  

                long ID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);  

                if (ID == Id) {  

                    Toast.makeText(getApplicationContext(), "任务:" + Id + " 下载完成!", Toast.LENGTH_LONG).show();  

                }  

            }  

        };  

  

        registerReceiver(broadcastReceiver, intentFilter);  

    }  

  

    @Override  

    public void onDestroy() {  

        super.onDestroy();  

        unregisterReceiver(broadcastReceiver);  

    }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐