欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android 使用registerReceiver注册BroadcastReceiver案例详解

时间:2022-01-21 09:44:18|栏目:Android代码|点击:

android.context.ContextWrapper.registerReceiver

public Intent registerReceiver (BroadcastReceiver receiver, IntentFilter filter) 

Register a BroadcastReceiver to be run in the main activity thread. The receiver will be called with any broadcast Intent that matches filter, in the main application thread.

Parameters

receiver  The BroadcastReceiver to handle the broadcast.
filter  Selects the Intent broadcasts to be received.

Returns

The first sticky intent found that matches filter, or null if there are none.

定义变量一

private SDStateMonitorReceiver sdStateReceiver; //SDcard状态监测
private LogTaskReceiver logTaskReceiver;

定义变量二

private static String MONITOR_LOG_SIZE_ACTION = "MONITOR_LOG_SIZE";        //日志文件监测action
private static String SWITCH_LOG_FILE_ACTION = "SWITCH_LOG_FILE_ACTION";    //切换日志文件action

定义变量三

private final int SDCARD_TYPE = 0;            //当前的日志记录类型为存储在SD卡下面
private final int MEMORY_TYPE = 1;            //当前的日志记录类型为存储在内存中
private int CURR_LOG_TYPE = SDCARD_TYPE;    //当前的日志记录类型

定义方法register

private void register(){
    IntentFilter sdCarMonitorFilter = new IntentFilter();
    sdCarMonitorFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
    sdCarMonitorFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
    sdCarMonitorFilter.addDataScheme("file");
    sdStateReceiver = new SDStateMonitorReceiver();
    registerReceiver(sdStateReceiver, sdCarMonitorFilter);
    
    IntentFilter logTaskFilter = new IntentFilter();
    logTaskFilter.addAction(MONITOR_LOG_SIZE_ACTION);
    logTaskFilter.addAction(SWITCH_LOG_FILE_ACTION);
    logTaskReceiver = new LogTaskReceiver();
    registerReceiver(logTaskReceiver,logTaskFilter);
}

定义BroadcastReceiver的子类SDStateMonitorReceiver

/**
 * 监控SD卡状态
 */
class SDStateMonitorReceiver extends BroadcastReceiver{
    public void onReceive(Context context, Intent intent) {
        
        if(Intent.ACTION_MEDIA_UNMOUNTED.equals(intent.getAction())){    //存储卡被卸载
            if(CURR_LOG_TYPE == SDCARD_TYPE){
                Log.d(TAG, "SDcar is UNMOUNTED");
                CURR_LOG_TYPE = MEMORY_TYPE;
                new LogCollectorThread().start();
            }
        }else{                                                            //存储卡被挂载
            if(CURR_LOG_TYPE == MEMORY_TYPE){
                Log.d(TAG, "SDcar is MOUNTED");
                CURR_LOG_TYPE = SDCARD_TYPE;
                new LogCollectorThread().start();
                
            }
        }
    }
}

定义BroadcastReceiver子类LogTaskReceiver

/**
 * 日志任务接收
 * 切换日志,监控日志大小
 */
class LogTaskReceiver extends BroadcastReceiver{
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(SWITCH_LOG_FILE_ACTION.equals(action)){
            new LogCollectorThread().start();
        }else if(MONITOR_LOG_SIZE_ACTION.equals(action)){
            checkLogSize();
        }
    }
}

在onCreate方法中调用register方法。

在onDestroy方法中执行语句

unregisterReceiver(sdStateReceiver);
unregisterReceiver(logTaskReceiver);

上一篇:Android自定义控件ViewFipper实现竖直跑马灯效果

栏    目:Android代码

下一篇:Flutter进阶之实现动画效果(十)

本文标题:Android 使用registerReceiver注册BroadcastReceiver案例详解

本文地址:http://www.codeinn.net/misctech/190911.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有