Android broadcast (note)

Source: Internet
Author: User

Broadcast is used in Android programs. There are two ways to register a broadcast receiver:
Method 1: register in the AndroidManifest. xml file
<Cycler android: name = "mycycler">
<Intent-filter>
<Action android: name = "com. and. android. intent. action. TEST"/>
<Action android: name = "android.net. wifi. WIFI_STATE_CHANGED"/>
</Intent-filter>
</Cycler>

You have registered your own broadcast receiver to receive two types of broadcasts. The first is a custom action, and the second is a system. When the WIFI status changes. All for testing, just add one.
This registration method is resident. When the application is closed (before it is uninstalled), the program runs automatically by the system call if the broadcast information is sent.
Method 2: dynamically register in the program code
BroadcastReceiver receiver ER = new MyReceiver ();
IntentFilter filter = new IntentFilter ();
Filter. addAction (Intent. ACTION_SCREEN_OFF); // Add the system broadcast here and send it when the screen is off.
Filter. addAction (ACTION_INTENT_TEST );
RegisterReceiver (receiver, filter );

This registration method is not resident, and the broadcast receiver follows the lifecycle of the Activity. Note: remove the register broadcast receiver before the Activity ends.

UnregisterReceiver (receiver );
If you forget to cancel the registration, it will not be retained in the system. That is, when broadcast comes, the program will not process it.

 

From continuing revolution

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.