The use of Android development Broadcastreceiver

Source: Internet
Author: User

1. Static registration.

Register in the manifest.

1 <  android:name= "Com.exce.learnbroadcastreceiver.MyReceiver"></  Receiver>

Then use the Sendbroadcast (Intent) method in the activity. Where the parameter is intent.

1 Intent intent=new Intent (This, myreceiver.  Class); 2 Intent.putextra ("Data", "Exer");   Carrying data 3 sendbroadcast (intent);

2. Dynamic registration.

You do not need to register in manifest.

Use Registerreceiver () and Unregisterreceiver () for broadcast registration and de-registration in your code.

Registerreceiver (broadcastreceiver receiver, Intentfilter filter), the first parameter is receiver, the second parameter is Intentfilter.

Unregisterreceiver (Broadcastreceiver Receiver), the parameter is receiver.

Code:

1 Private Myreceiver receiver;
...
1  CaseR.id.regbroadcast:2 if(receiver==NULL) {3Receiver=Newmyreceiver ();4Registerreceiver (receiver,NewIntentfilter (myreceiver.action));5     }6      Break;7  CaseR.id.unregbroadcast:8 if(receiver!=NULL) {9 Unregisterreceiver (receiver);TenReceiver=NULL; One}

Myreceiver:

1  Public Static Final String action= "Com.exec.learnbroadcastreceiver.intent.action";

Intentfilter is obtained by constant action,new Intentfilter (myreceiver.action). Then you can register for the broadcast.

Dynamic registration of the broadcast, must be destroyed at the time of the cancellation of registration. That is, Registerreceiver () and Unregisterreceiver () must appear at the same time.

Priority of 3.Broadcast

The Broadcastreceiver priority can be obtained through the android:priority= "" property of Intent-filter in manifest. Android:priority= "9" need to fill in a number, the larger the number, the higher the priority.

for an ordered broadcast , you can use the Sendorderedbroadcast (Intent Intent, String receiverpermission) method, and then mate with the android:priority= "" Property, Make an orderly broadcast

If you want to truncate the broadcast , you can truncate the broadcast by using the Abortbroadcast () method in the OnReceive () method of the Myreceiver that inherits Broadcastreceiver.

The use of Android development Broadcastreceiver

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.