Android Broadcast Demo

Source: Internet
Author: User

Specific steps:

1. Initialize the broadcast receiver Broadcastreceiver, and the received broadcast will be recalled in the OnReceive method of the class.

2, registered broadcast registerreceiver(broadcastreceiver,intentfilter);

3, send broadcast sendbroadcast (Intent);


This demo is to verify that the activity that has been overwritten can update the UI itself when the broadcast is received, following the specific code:

The first step is to create the mainactivity and initialize the broadcast receiver Broadcastreceiver, register the broadcast

Public class mainactivity extends appcompatactivity {    private  textview tiptv;    public static final string action_name  =  "Tag_main";     @Override     protected void  OnCreate (bundle savedinstancestate)  {        super.oncreate ( Savedinstancestate);         setcontentview (R.layout.activity_main);         tipTv =  (TextView)  findviewbyid (R.ID.TIPTV);         findviewbyid (R.ID.GOTOTV). Setonclicklistener (new  View.onclicklistener ()  {             @Override             public void onclick (View  V)  {   &NBsp;            intent intent = new  intent (Mainactivity.this, secondactivity.class);                 startactivityforresult (intent, 100);                 tiptv.settext ("Waiting to receive broadcast ... ");            }                  //Registered Radio          registerboradcastreceiver ();     }    //broadcast receiver      private BroadcastReceiver mBroadcastReceiver = new  Broadcastreceiver ()  {        /**          *  broadcast receiver, receive the broadcast callback method          *  @param  context          *  @param  intent         */          @Override         public  void onreceive (context context, intent intent)  {             LOG.D ("MYP",  "=== broadcast callback ===");             String action =  Intent.getaction ();            if  ( Action.equals (action_name))  {                 //when a broadcast is received, the toast is prompted in the topmost activity,                 toast. Maketext (mainactivity.this,  "Broadcast callback",  toast.length_short). Show ();                 string result =  intent.getstringextra ("Yaner");                 tiptv.settext (Result);                 //when a broadcast is received, dialog will receive a broadcast interface prompt and will be hidden with the business logic in that interface,                 //  This shows that when the activity is covered, If the activity business logic is started again, it will execute normally in the activity                  initdialog ();                 timer timer = new timer ();                  timer.schedule (New timertask ()  {                      @Override                      public  Void run ()  {                         if  (dialog != null &&  dialog.isshowing ())                              dialog.dismiss ();                      }                },  3 * 1000);             }         }    };    Dialog dialog;    /**      *  Initialize pop-up box      */    private void  initdialog ()  {        dialog = new dialog ( this);         dialog.setcontentview (R.layout.view_dialog);         dialog.show ();    }    /**      *  Registered broadcast      */    public void  registerboradcastreceiver ()  {        IntentFilter  Filer = new intentfilter ();//Filter conditions          Filer.addaction (action_name);//Set up a broadcast to registerThe label         //registered Radio          Registerreceiver (Mbroadcastreceiver, filer);         LOG.D ("MYP",   "=== broadcast regist ===");     }}


The second step is to create the secondactivity, and the secondactivity implementation trigger can send the broadcast to

The radio events in Mainactivity.

public class secondactivity extends activity {     @Override      protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview (R.layout.activity_second);         findviewbyid ( R.ID.BRODCASTBTN). Setonclicklistener (New view.onclicklistener ()  {              @Override              public void onclick (VIEW V)  {                 intent mintent = new intent ( Mainactivity.action_name);                 mintent.putextra ("Yaner",  "Receive broadcast success!!! )///When receiving a broadcast, carry data                  //Send broadcast                  Sendbroadcast (mintent);                 LOG.D ("MYP",  "=== broadcast send ===");             }        });     }}


XML layout file is relatively simple, it is not given, the effect of running:

1, click the button, Toast prompt in secondactivity directly prompt

2, click the button, in 3 seconds return mainactivity,dialog normal display, 3 seconds after the disappearance

3, after the click button, after 3 seconds to return Mainactivity,dialog has disappeared


Conclusion: The covered activity can update the UI itself when it receives the broadcast

This article is from the "10924237" blog, please be sure to keep this source http://10934237.blog.51cto.com/10924237/1726810

Android Broadcast Demo

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.