Four android components: BroadcastReceiver and broadcastreceiver

Source: Internet
Author: User

Four android components: BroadcastReceiver and broadcastreceiver

Today, I reviewed my previous study notes and summarized my BroadcastReceiver for my reference.

BroadcastReceiver is a global listener for android systems. It can communicate with different components.

Create your own broadcast class

package com.example.yummyhttputil; import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.IBinder; public class MyBoradcastReceiver extends BroadcastReceiver {     /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub     }     @Override    public void onReceive(Context context, Intent intent) {        // TODO Auto-generated method stub             }     @Override    public IBinder peekService(Context myContext, Intent service) {        // TODO Auto-generated method stub        return super.peekService(myContext, service);    } }


Register static broadcast Registration
<receiver android:name=".MyBoradcastReceiver ">              <intent-filter>                  <action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>                  <category android:name="android.intent.category.DEFAULT" />              </intent-filter>  </receiver>  
After the above information is configured, as long as it is broadcast of the android. intent. action. MY_BROADCAST address, MyReceiver can receive it. Note that this method of registration is resident. That is to say, when the application is closed, if the broadcast information is sent, MyReceiver will be called by the system and run automatically.
Dynamic Registration
MyBoradcastReceiver receiver = new MyBoradcastReceiver ();           IntentFilter filter = new IntentFilter();  filter.addAction("android.intent.action.MYBROADCAST_RECEIVER");        registerReceiver(receiver, filter);  
Note that registerReceiver is a method in the android. content. ContextWrapper class. Both Activity and Service inherit ContextWrapper, so you can call it directly. In practical applications, we have registered a BroadcastReceiver in Activity or Service. If this Activity or Service is destroyed, the system reports an exception, prompt whether we forgot to cancel the registration. Therefore, remember to perform the unregister operation in a specific place:
@Override  protected void onDestroy() {      super.onDestroy();      unregisterReceiver(receiver);  }  


Send Broadcast
public void send(View view) {      Intent intent = new Intent("android.intent.action.MYBROADCAST_RECEIVER");      intent.putExtra("msg", "hello yummylau!");      sendBroadcast(intent);  } 


Broadcast type Normal Broadcast (Normal Broadcast) Normal Broadcast is completely asynchronous for multiple receivers. Generally, each receiver can receive the Broadcast without waiting, and the receiver will not affect each other. For such broadcast, the receiver cannot terminate the broadcast, that is, it cannot block the receiving actions of other receivers.
Ordered Broadcast (Ordered Broadcast) is special. It is sent only to the receiver with a higher priority, and then transmitted to the receiver with a lower priority, the receiver with a higher priority has the ability to terminate the broadcast.
Set three broadcast acceptance classes to determine who needs to consume the broadcast first by setting the priority.
<receiver android:name=".FirstReceiver">      <intent-filter android:priority="1000">          <action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>          <category android:name="android.intent.category.DEFAULT" />      </intent-filter>  </receiver>  <receiver android:name=".SecondReceiver">      <intent-filter android:priority="999">          <action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>          <category android:name="android.intent.category.DEFAULT" />      </intent-filter>  </receiver>  <receiver android:name=".ThirdReceiver">      <intent-filter android:priority="998">          <action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>          <category android:name="android.intent.category.DEFAULT" />      </intent-filter>  </receiver>  

The <intent-filter> of the three receivers has an android: priority attribute, which is reduced in turn. This attribute ranges from-1000 to 1000. A greater value indicates a higher priority.
Send ordered Broadcast

public void send(View view) {      Intent intent = new Intent("android.intent.action.MYBROADCAST_RECEIVER");      intent.putExtra("msg", "hello yummylau!");      sendOrderedBroadcast(intent, "scott.permission.MY_BROADCAST_PERMISSION");  } 

Note: When the sendOrderedBroadcast method is used to send an ordered broadcast, a permission parameter is required. If it is null, the recipient is not required to declare the specified permission. If it is not null, it indicates that if the recipient wants to receive the broadcast, the specified permission must be declared. This is from the security perspective. For example, system text messages are in the form of ordered broadcasting. An application may be capable of intercepting spam messages, when a text message arrives, it can receive the text message broadcast first. If necessary, the broadcast transmission is terminated. Such software must declare the permission to receive the text message.
Therefore, we define a permission in AndroidMainfest. xml:
<permission android:protectionLevel="normal"              android:name="scott.permission.MYBROADCAST_RECEIVER_PERMISSION" /> 
Then declare this permission
<uses-permission android:name="scott.permission.MYBROADCAST_RECEIVER_PERMISSION" />  
If the first broadcast needs to add messages to the second broadcast, you can add messages in onReceive ().
Bundle bundle = new Bundle (); bundle. putString ("addString", "I am the added message"); setResultExtras (bundle );
In the second broadcast category
Bundle bundle = getResultExtras(true);String add = bundle.getString("addString");
Cancel continue Broadcast
abortBroadcast();  












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.