"Android" several common broadcast listeners (WIFI, light off screen, home key, SMS) summary

Source: Internet
Author: User

Broadcast receiver for WiFi status monitoring
/** * Monitor the WiFi status of the broadcast receiver, note that you do not need to add any permissions */public final class Wifistatereceiver extends Broadcastreceiver {private static FINA    L 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, "unknown state");            Break                Case WIFIMANAGER.WIFI_STATE_ENABLING:LOG.I (TAG, "WiFi is Connected");            Break                Case WIFIMANAGER.WIFI_STATE_ENABLED:LOG.I (TAG, "WIFI available");            Break                Case WIFIMANAGER.WIFI_STATE_DISABLING:LOG.I (TAG, "WiFi is disconnecting");            Break                Case WIFIMANAGER.WIFI_STATE_DISABLED:LOG.I (TAG, "WIFI unavailable");            Break        Default:break; }}}/** * Register broadcast receiver */private void Registreceiver () {    Receiver = new Wifistatereceiver ();    Intentfilter filter = new Intentfilter (wifimanager.wifi_state_changed_action); Registerreceiver (receiver, filter);}
Screen off to monitor with light screen

When the screen is off to light screen will be sent two broadcast screen_on and Screen_off, but these two action** can only be registered in the form of code to be able to be listened to, in the Androidmanifest.xml register at all.

/** * light off screen monitoring, do not need any permissions, must code registration, Androidmanifest.xml registration Invalid */public class Screenactionreceiver extends Broadcastreceiver {priv    Ate 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, "the screen unlocks the broadcast (that is, the screen is lit) ...");        } else if (Action.equals (Intent.action_screen_off)) {log.i (TAG, "screen lock broadcast (i.e. off screen) ..."); }}/** * Broadcast Register * * @param mcontext Context Object */public void Registerscreenactionreceiver (context MCon            Text) {if (!isregisterreceiver) {isregisterreceiver = true;            Intentfilter filter = new Intentfilter ();            Filter.addaction (Intent.action_screen_off);            Filter.addaction (intent.action_screen_on);            LOG.I (TAG, "registration screen unlock, lock broadcast receiver ..."); Mcontext.registerreceiver (screenactionreceiveR.this, filter); }}/** * Broadcast logoff * * @param mcontext Context Object */public void Unregisterscreenactionreceiver (context MC            Ontext) {if (isregisterreceiver) {isregisterreceiver = false;            LOG.I (TAG, "logout screen unlock, lock broadcast receiver ...");        Mcontext.unregisterreceiver (Screenactionreceiver.this); }    }}
Home key Monitoring
/** * Home key is a system button, we can not intercept through onkeydown, it is intercept not * to, we can only get when he was pressed. is through the broadcast receiver, this broadcast also does not require any permissions */public class Homekeyeventbroadcastreceiver extends Broadcastreceiver {private String TAG = "Ta    Gforhomekeyeventbroadcastreceiver ";    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 processing point                LOG.I (TAG, "Click the Home button"); } else if (Reason.equals (System_recent_apps)) {//Long home key handles the point log.i (TAG, "long pressed H                ome key "); }            }}}/** * Broadcast Register * * @param mcontext Context Object */public void Register (context MCo            ntext) {if (!isregisterreceiver) {isregisterreceiver = true;            Intentfilter filter = new Intentfilter ();            Filter.addaction (Intent.action_close_system_dialogs);        Mcontext.registerreceiver (homekeyeventbroadcastreceiver.this, filter); }}/** * Broadcast logoff * * @param mcontext Context Object */public void Unregisterscreenactionreceiver (context MC            Ontext) {if (isregisterreceiver) {isregisterreceiver = false;        Mcontext.unregisterreceiver (Homekeyeventbroadcastreceiver.this); }    }}
SMS Broadcast recipient (SMS blocker possible)

The Android system sends an ordered broadcast when it receives a text message, and if we define a recipient to receive the broadcast, we can get the message or intercept the text.
Define broadcast receivers to receive broadcast Android.provider.Telephony.SMS_RECEIVED
Need to receive SMS permission:
SMS notification in Android system is an orderly broadcast , we need to intercept spam messages, you can configure a higher priority, receive information to determine whether Abortbroadcast (), that is, stop the broadcast continue low priority broadcast

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;            }        }    }}

Welcome Androider Attention Public Number: Love Android

"Android" several common broadcast listeners (WIFI, light off screen, home key, SMS) summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.