Android Development about broadcast Receiver

Source: Internet
Author: User



Broadcast receiver is one of the most commonly used Android technologies

For sending/Broadcasting intent messages

Here are two simple ways to use it


I. By way of combination

Defining a Broadcastreceiver object in a class

Rewrite onreceive so you can receive the broadcast

This can be put into the general activity class.


First register Intentfilter Specify the message to receive

Register a intentfilter        intentfilter intentfilter =  new Intentfilter ("Playhandler.broadcast");         

Then define the Broadcastreceiver object to receive the message

Private Broadcastreceiver Mybroadcastreceiver = new Broadcastreceiver () {         @Override         public   void   OnReceive (context context, Intent Intent) {         //Receive broadcast    //Note here is the main thread on     System.out.println ("Thread ID:" + Thread.CurrentThread (). GetId ());        System.out.println ("Playhandler.broadcast onreceived!!");        }     

Such an object that receives message processing is completed.

Below to send a message


Use Sendbroadcast to send broadcast    Intent Intent = new Intent ("Playhandler.broadcast");     Sendbroadcast (Intent);     

Two. Registering the XML

Unlike the previous method, the manifest is used to register receiver

Need to inherit Broadcastreceiver class


First register in Manifest

<receiver     android:exported= "false"    android:name= "Com.example.codetest_1.PlayBroadcastReceiver" >  <intent-filter>     <action android:name = "Playbroadcastreceiver.broadcast"/>  </ Intent-filter> </receiver>


Declaring a broadcastreceiver derived class

public class Playbroadcastreceiver extends Broadcastreceiver {public static final String broadcast_action  =  " Playbroadcastreceiver.broadcast "; @Overridepublic void OnReceive (Context arg0, Intent arg1) {//TODO auto-generated method StubSystem.out.println (" Playbroadcastreceiver.broadcast onreceived!! "); }}

The message is sent in the same way as the first method

Intent = new Intent ();        Intent.setaction (playbroadcastreceiver.broadcast_action);    Sendbroadcast (intent);     System.out.println ("Playbroadcastreceiver.broadcast send");



Android Development about broadcast Receiver

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.