• Android 进价5_自定义广播 通过广播更新ListView的适配器 下载管理


      1、在处理下载管理时,服务在后台运行,下载完成后要更新listview列表的按钮,将“下载”改成“打开”这样一个功能。

      在Activity里面写一个静态内部类,继承广播。其中属性text_button的值就是按钮显示的文字。通过mAdapter.notifyDataSetChanged()更新列表数据显示。

        public static class DownloadFinishReceiver extends BroadcastReceiver{
    
            public static final String DOWNLOAD_RECEIVER="com.DownloadFinishReceiver";
            
            @Override
            public void onReceive(Context context, Intent intent) {
                try {
                    if (intent.getAction().equals(DOWNLOAD_RECEIVER)) {
                        //传递的下载地址
                        String url = intent.getStringExtra("url");
                        for(Apply apply : mApplies) {
                            if (apply.getDownload_backup().equals(url)) {
                                apply.setText_button("打开");
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

      2、在后台服务service里面注册和销毁广播

        private DownloadFinishReceiver finishReceiver;
        public void onCreate() {
            System.out.println("创建服务");
            IntentFilter intentFilter = new IntentFilter(DownloadFinishReceiver.DOWNLOAD_RECEIVER);
            finishReceiver =  new DownloadFinishReceiver();
            registerReceiver(finishReceiver, intentFilter);
            super.onCreate();
        }
    
        public void onDestroy() {
            unregisterReceiver(finishReceiver);
            super.onDestroy();
        }

      3、一旦下载完成,就发送广播。

    Intent intent = new Intent(DownloadFinishReceiver.DOWNLOAD_RECEIVER);
    intent.putExtra("url", "http://www.baidu.com/pic/qqq.png");
    sendBroadcast(intent);
  • 相关阅读:
    基于p2p聊天室的原理介绍.个人学习笔记
    一个可移植数据库操作组件的简单介绍
    常见任务
    sql常用语句
    认真写写SQLAlchemy
    Jenkins 安装与使用手册
    Ajax
    支持主流注册中心,SolarMesh发布新版本 SolarMesh
    API标准化对Dapr的重要性
    企业数字化转型,你知道有哪些关键要素吗?
  • 原文地址:https://www.cnblogs.com/lbangel/p/3938136.html
Copyright © 2020-2023  润新知