欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 使用registerReceiver注冊BroadcastReceiver案例詳解

 更新時(shí)間:2021年08月26日 08:50:25   作者:習(xí)以常  
這篇文章主要介紹了Android 使用registerReceiver注冊BroadcastReceiver案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

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狀態(tài)監(jiān)測
private LogTaskReceiver logTaskReceiver;

定義變量二

private static String MONITOR_LOG_SIZE_ACTION = "MONITOR_LOG_SIZE";        //日志文件監(jiān)測action
private static String SWITCH_LOG_FILE_ACTION = "SWITCH_LOG_FILE_ACTION";    //切換日志文件action

定義變量三

private final int SDCARD_TYPE = 0;            //當(dāng)前的日志記錄類型為存儲(chǔ)在SD卡下面
private final int MEMORY_TYPE = 1;            //當(dāng)前的日志記錄類型為存儲(chǔ)在內(nèi)存中
private int CURR_LOG_TYPE = SDCARD_TYPE;    //當(dāng)前的日志記錄類型

定義方法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

/**
 * 監(jiān)控SD卡狀態(tài)
 */
class SDStateMonitorReceiver extends BroadcastReceiver{
    public void onReceive(Context context, Intent intent) {
        
        if(Intent.ACTION_MEDIA_UNMOUNTED.equals(intent.getAction())){    //存儲(chǔ)卡被卸載
            if(CURR_LOG_TYPE == SDCARD_TYPE){
                Log.d(TAG, "SDcar is UNMOUNTED");
                CURR_LOG_TYPE = MEMORY_TYPE;
                new LogCollectorThread().start();
            }
        }else{                                                            //存儲(chǔ)卡被掛載
            if(CURR_LOG_TYPE == MEMORY_TYPE){
                Log.d(TAG, "SDcar is MOUNTED");
                CURR_LOG_TYPE = SDCARD_TYPE;
                new LogCollectorThread().start();
                
            }
        }
    }
}

定義BroadcastReceiver子類LogTaskReceiver

/**
 * 日志任務(wù)接收
 * 切換日志,監(jiān)控日志大小
 */
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方法中調(diào)用register方法。

在onDestroy方法中執(zhí)行語句

unregisterReceiver(sdStateReceiver);
unregisterReceiver(logTaskReceiver);

到此這篇關(guān)于Android 使用registerReceiver注冊BroadcastReceiver案例詳解的文章就介紹到這了,更多相關(guān)Android 使用registerReceiver注冊BroadcastReceiver內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論