Android 使用registerReceiver註冊BroadcastReceiver案例詳解

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 使用registerReceiver註冊BroadcastReceiver案例詳解的文章就介紹到這瞭,更多相關Android 使用registerReceiver註冊BroadcastReceiver內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: