About Android Receiver

Source: Internet
Author: User
<span id="Label3"></p>You can declare a receiver in a code file, or you can declare in manifest that receiver in the former only listens for events that are of interest to the activity when it Launch. And if it is declared in androidmanifext.xml, it is unrestricted to listen to events of interest at any Time. 1.<br>First of all talk about registering a receiver in the androidmanifext.xml, for example we want to monitor the camera button press event, and then call our camera program when it happens.<br><receiver android:name= "camerabuttonintentreceiver" > <intent-filter> You can declare a receiver in a code file, or in the MA In nifest, the former receiver only listens to events that are of interest only after the activity launch, and if declared in androidmanifext.xml, it is free to listen to events of interest at any Time. 1.<br>First of all talk about registering a receiver in the androidmanifext.xml, for example we want to monitor the camera button press event, and then call our camera program when it happens.<br><receiver android:name= "camerabuttonintentreceiver" > <intent-filter> <action an Droid:name= "android.intent.action.CAMERA_BUTTON"/> </intent-filter> </receiver><br>In addition, when the program needs to use the phone-related services, such as telephone, sms, Internet and other functions, you have to add the user-permission in the Manifest.<br>In this configuration file, a receiver is used to monitor the Camera's key event, so you also need to define receiver in the code file with the same name as the configuration file:<br>public class Camerabuttonintentreceiver extends Broadcastreceiver {<br>public void OnReceive (context context, Intent Intent) {Intent i = new Intent (intent.action_main); I.setclass (context, camera.class);<br>I.setflags (intent.flag_activity_new_task); Context.startactivity (i); } }<br>2. For another, register a receiver in the code, for example, if we want to listen for changes in the battery level in the code file, we can do this by private final broadcastreceiver mbatteryreceiver = new Bro Adcastreceiver () {@Override public void onreceive (context context, Intent Intent) {<br>String action = intent.getaction (); If (action.equals (INTENT.ACTION_BATTERY_CHANGED)) {...<br>}         } }<br>This method requires registering the Receiver's Intent in OnCreate or onstart, such as: registerreceiver (mbatteryreceiver, intent.action_battery_ CHANGED);<br>Log out of monitoring in OnStop and OnDestroy<br>Unregisterreceiver (mbatteryreceiver, intent.action_battery_changed);<br><br>System broadcasts can capture the behavior that the system Emits: 1. "android.provider.Telephony.SMS_RECEIVED" Received SMS 2. Intent.action_battery_changed the remaining cell phone battery capacity of 3. Intent.action_media_mounted SD card mounted successfully 4. intent.action_media_unmounted SD Card not mounted<br>5. Intent.action_new_outgoing_call Call 6. intent.action_package_added performing the installation 7. Intent.action_package_removed performing the uninstallation<p><p>About Android Receiver</p></p></span>

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.