【Android】几种常见广播监听器(Wifi,亮灭屏,Home键,短信)总结

Wifi状态监听的广播接收者

/**
 * 监控Wifi状态的广播接收器,注意不需要添加任何权限
 */
public final class WifiStateReceiver extends BroadcastReceiver {
    private static final String TAG = "TagForWifiStateReceiver";

    @Override
    public void onReceive(Context c, Intent intent) {
        Bundle bundle = intent.getExtras();
        int statusInt = bundle.getInt("wifi_state");
        switch (statusInt) {
            case WifiManager.WIFI_STATE_UNKNOWN:
                Log.i(TAG, "未知状态");
                break;
            case WifiManager.WIFI_STATE_ENABLING:
                Log.i(TAG, "wifi正在连接");
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                Log.i(TAG, "wifi可用");
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                Log.i(TAG, "wifi正在断开连接");
                break;
            case WifiManager.WIFI_STATE_DISABLED:
                Log.i(TAG, "wifi不可用");
                break;
            default:
                break;
        }
    }
}

/**
 * 注册广播接收器
 */
private void registReceiver() {
    receiver = new WifiStateReceiver();
    IntentFilter filter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
    registerReceiver(receiver, filter);
}

屏幕灭屏以亮屏监听

屏幕灭屏以亮屏时会分别发送两个广播SCREEN_ON和SCREEN_OFF,但是这两个action**只能通过代码的形式注册**才能被监听到,在AndroidManifest.xml中注册根本监听不到。

/**
 * 亮灭屏监听,不需要任何权限,必须代码注册,AndroidManifest.xml注册无效
 */
public class ScreenActionReceiver extends BroadcastReceiver {

    private String TAG = "TagForScreenActionReceiver";
    private boolean isRegisterReceiver = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_SCREEN_ON)) {
            Log.i(TAG, "屏幕解锁广播(即亮屏了)...");
        } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
            Log.i(TAG, "屏幕加锁广播(即灭屏了)...");
        }
    }

    /**
     * 广播注册
     *
     * @param mContext 上下文对象
     */
    public void registerScreenActionReceiver(Context mContext) {
        if (!isRegisterReceiver) {
            isRegisterReceiver = true;
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            filter.addAction(Intent.ACTION_SCREEN_ON);
            Log.i(TAG, "注册屏幕解锁、加锁广播接收者...");
            mContext.registerReceiver(ScreenActionReceiver.this, filter);
        }
    }

    /**
     * 广播注销
     *
     * @param mContext 上下文对象
     */
    public void unRegisterScreenActionReceiver(Context mContext) {
        if (isRegisterReceiver) {
            isRegisterReceiver = false;
            Log.i(TAG, "注销屏幕解锁、加锁广播接收者...");
            mContext.unregisterReceiver(ScreenActionReceiver.this);
        }
    }
}

Home键监听

/**
 * Home键是一个系统的按钮,我们无法通过onKeyDown进行拦截,它是拦截不
 * 到的,我们只能得到他在什么时候被按下了。就是通过广播接收者,此广播也不需要任何权限
 */
public class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {
    private String TAG = "TagForHomeKeyEventBroadCastReceiver";
    static final String SYSTEM_REASON = "reason";
    static final String SYSTEM_HOME_KEY = "homekey";
    static final String SYSTEM_RECENT_APPS = "recentapps";
    private boolean isRegisterReceiver = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
            String reason = intent.getStringExtra(SYSTEM_REASON);
            if (reason != null) {
                if (reason.equals(SYSTEM_HOME_KEY)) {
                    // home key处理点
                    Log.i(TAG, "点击了Home键");
                } else if (reason.equals(SYSTEM_RECENT_APPS)) {
                    // long home key处理点
                    Log.i(TAG, "长按了Home键");
                }
            }
        }
    }

    /**
     * 广播注册
     *
     * @param mContext 上下文对象
     */
    public void register(Context mContext) {
        if (!isRegisterReceiver) {
            isRegisterReceiver = true;
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            mContext.registerReceiver(HomeKeyEventBroadCastReceiver.this, filter);
        }
    }

    /**
     * 广播注销
     *
     * @param mContext 上下文对象
     */
    public void unRegisterScreenActionReceiver(Context mContext) {
        if (isRegisterReceiver) {
            isRegisterReceiver = false;
            mContext.unregisterReceiver(HomeKeyEventBroadCastReceiver.this);
        }
    }
}

短信广播接收者(可以进行短信拦截)

Android系统在收到短信的时候会发送一条有序广播,我们如果定义一个接收者接收这个广播,就可以得到短信内容,也可以拦截短信。
定义广播接收者接收广播android.provider.Telephony.SMS_RECEIVED
需要接收短信权限:
Android系统中收到短信的通知是一个有序广播,我们如需拦截垃圾短信,可以配置较高的Priority(优先级),收到信息进行判断是否abortBroadcast(),即中止广播继续低优先级广播

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] pdus = (Object[]) intent.getExtras().get("pdus");      // 获取短信数据(可能有多段)
        for (Object pdu : pdus) {
            SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdu);    // 把短信数据封装成SmsMessage对象
            Date date = new Date(sms.getTimestampMillis());             // 短信时间
            String address = sms.getOriginatingAddress();               // 获取发信人号码
            String body = sms.getMessageBody();                         // 短信内容

            System.out.println(date + ", " + address + ", " + body);    
            if ("18600012345".equals(address)) {
                abortBroadcast();
                return;
            }
        }
    }
}

欢迎androider关注微信公众号:爱安卓
这里写图片描述

你可能感兴趣的:(Android开发)