Basic course of Android mobile application [broadcast mechanism]

Usually, each classroom in the school will be equipped with a speaker, which is connected to the school broadcasting room. If there is an important notice, a radio will be sent to inform the whole school. In order to facilitate sending and receiving system level message notifications, Android system also introduces a set of message mechanism similar to broadcasting.

The broadcast mechanism in Android is used for inter process / thread communication. The mechanism uses the observer mode. The observer mode is a software design mode. The mode is a message based publish / subscribe event model. The message publisher in the model is the broadcast sender in the broadcast mechanism, and the message subscriber is the broadcast receiver in the broadcast mechanism, The specific implementation process of broadcast mechanism is shown in the figure below.

public class MyReceiver extends BroadcastReceiver {
           public MyReceiver() {
           }
           @Override
           //在该方法中实现广播接收者的相关操作
            public void onReceive (Context context, Intent intent) {
                 throw new UnsupportedOperationException("Not yet implemented");
            }
     }

 receiver = new MyBroadcastReceiver(); //实例化广播接收者
     //实例化过滤器并设置要过滤的广播
    String action = "android.provider.Telephony.SMS_RECEIVED";
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(action);
    registerReceiver(receiver,intentFilter); //注册广播

 		//动态注册MyReceiver广播
        MyReceiver  one = new MyReceiver ();
        IntentFilter filter = new IntentFilter();
        //数值越大,优先级越高。如果两个广播接收者的优先级相同,则先注册的广播接收者优先级高。
        filter.setPriority(1000); 
        filter.addAction("Intercept_Stitch");
        registerReceiver(one,filter);

This chapter explains the relevant knowledge of broadcast recipients in detail. First, it introduces what broadcast recipients are, and then explains how to customize broadcasts and broadcast types. Through the study of this chapter, beginners are required to master the use of broadcast receivers and apply them in practical development.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>