Beginner Android,broadcastreceiver send and receive broadcasts

Source: Internet
Author: User

Broadcastreceiver is used to listen to the system global broadcast message, because Broadcastreceiver is a global listener, so it can be very convenient to implement communication between the different components of the system

It takes two steps to start it.

1. Create a broadcastreceiver intent that needs to be started

2. Invoke the Sendbroadcast or Sendorederedbroadcast method of the context to start the specified broadcastreceiver

The key is to setaction ("xxx") when creating intent, and broadcastreceiver to create its filter, or specify it in the code, or specify it in the XML

Here is a simple example of a program that sends messages to a broadcast

Sender-Side Main interface code

[Java]View Plaincopyprint?
  1. Package WangLi.Service.Broadcast;
  2. Import android.app.Activity;
  3. Import android.content.Intent;
  4. Import Android.os.Bundle;
  5. Import Android.view.View;
  6. Import Android.view.View.OnClickListener;
  7. Import Android.widget.Button;
  8. Public class broadcast extends Activity {
  9. Button send;
  10. @Override
  11. public void OnCreate (Bundle savedinstancestate) {
  12. super.oncreate (savedinstancestate);
  13. Setcontentview (R.layout.main);
  14. //Get buttons in the interface
  15. Send = (Button) Findviewbyid (r.id.send);
  16. Send.setonclicklistener (new Onclicklistener () {
  17. @Override
  18. public void OnClick (View arg0) {
  19. //Create Intent Object
  20. Intent Intent = new Intent ();
  21. //Set the Action property of Intent
  22. Intent.setaction ("wangli.test");
  23. Intent.putextra ("msg", "simple Message");
  24. //Send broadcast
  25. Sendbroadcast (Intent);
  26. }
  27. });
  28. }
  29. }

Then create a Broadcastreceiver listener

[Java]View Plaincopyprint?
  1. Package WangLi.Service.Broadcast;
  2. Import Android.content.BroadcastReceiver;
  3. Import Android.content.Context;
  4. Import android.content.Intent;
  5. Import Android.widget.Toast;
  6. Public class Myreceiver extends Broadcastreceiver {
  7. @Override
  8. public void OnReceive (context context, Intent Intent) {
  9. Toast.maketext (
  10. Context
  11. "The action received by intent is:" + intent.getaction () + "\ nthe message content is:"
  12. + Intent.getstringextra ("msg"), . Show ();
  13. }
  14. }

The receiver is configured to Androidmanifest.xml, primarily intent filter

[HTML]View Plaincopyprint?
    1. <receiver android:name= "myreceiver"  >   
    2.     < intent-filter>  
    3.         < Action android:name= "wangli.test"   />  
    4.     </intent-filter>   
    5. </receiver< span class= "tag" >>  

Note that Broadcastreceiver has an expiration time of 10 seconds, and if a time-consuming operation should be done in the service, Broadcastreceiver is only suitable for short-term computing tasks, because its life cycle is very short

Beginner Android,broadcastreceiver send and receive broadcasts

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.