Android broadcast infrastructure Depth - watching an in-depth understanding of the broadcast

First, the broadcast registration

  • Dynamic registration code registration.
  • Static registration AndroidManifest.XML file registration.

Difference:

Register to add a dynamic internal class inherits from BroadcaseReceiver at the event.

Static registration to create a new class that inherits from BroadcastReceiver and add tags AndroidManifest.XML file.

1.1 Dynamic registration

Dynamic monitor network changes

public class MainActivity extends AppCompatActivity{
	private TestReceiver mTestReceiver;
	
	@Override
	protected void onCreate(Bundle savedInstaceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//创建一个过滤器来获取需要监听的内容
		IntentFilter filter = new IntentFilter();
		filter.addAction("");
		//action不一定非要系统定义的aciton,也可以自己创建
		
		//在onCreate内部有了一个内部类必须通过new 才能获取,而由于TestReceiver继承自BroadcastReceiver,父类有一个默认的无参构造来为其创建
		mTestReceiver = new TestReceiver();
		regiseter(mTestReceiver, filter);
	}
	
	@Override
	protected void onDestroy(){
		unregister(mTestReceiver);
		super.onDestroy();
	}
	
	//监听网络变化
	class TestReceiver extends BroadCastReceiver{
	
		@Override
		public void onReceived(Context context, Intent intent){		
			//网络变化需要由系统服务监听,因此需要有一个系统服务类ConnectivityManager,专门用于管理网络连接
			//通过网络变化管理器来获取网络信息,网络变化的监听需要获取权限
			//在对网络信息进行处理
			ConnectivityManager connectivity = getSystemService(Context.CONNECTIVITY_SERVICE);
			Network networkInfo = connectivityManager.getActiveNetwork();
            	if (networkInfo != null) {
					//
            	} else {
					//
            	}
		}
	}
}

1.2 static registration

And the difference between dynamic registration: can be found in the dynamic process of listening,

1, it is necessary to get the content needs to listen through IntentFilter, registered in the broadcast through register,

2, create inner classes inside Acitivity to achieve dynamic monitoring.

The static registration, sucked the conversion process:

IntentFilter placed in AndroidManifest.XML file tab acquisition, for safety reasons, usually exported is set to false.

<receiver
	android:name=".TestReceiver"
	//enabled表示是否启用这个这个广播接收器
	android:enabled="true"
	//exported表示是否允许这个广播接收器接受本程序之外的广播
	android:exported="true">
	<Intent-filter>
		<action android:name=""/>
	</Intent-filter>
</reveiver>

Create an internal process of the class is to create a java class and inherit BroadcastReciver, and rewrite onReceive to achieve static registration.

BroadcastReceive create a shortcut by AS.

public class TestReceiver extends BroadCastReceiver{
	//监听网络变化
	@Override
	public void onReceived(Context context, Intent intent){		
		//网络变化需要由系统服务监听,因此需要有一个系统服务类ConnectivityManager,专门用于管理网络连接
		//通过网络变化管理器来获取网络信息,网络变化的监听需要获取权限
		//在对网络信息进行处理
		ConnectivityManager connectivity = getSystemService(Context.CONNECTIVITY_SERVICE);
		Network networkInfo = connectivityManager.getActiveNetwork();
           	if (networkInfo != null) {
				//
           	} else {
				//
           	}
	}
}

You need to call in broadcasting activities, you need to

public class MainActivity extends AppCompatActivity{
	private Button mBtnSendBroadcast;
	
	@Override
	protected void onCreate(Bundle savedInstaceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//若需要手动点击发送广播
		mBtnSendBroadcast = findViewById(R.id.button);
		mBtnSendBroadcast.setOnclickListener(new View.OnclickListener){
			@Override
			public void onClick(View v){
				Intent intent = new Intent("");
				sendBroadcast(intent);
			}
		}
	}
}

And registration can also be achieved using a static boot, boot broadcast monitoring system also needs permission android.permission.RECEIVE_BOOT_COMPLETED.

note:

Do not add too much logic or any time-consuming operation onReceive () method, because the broadcast receiver is not allowed to open threads. More radio receivers is one kind of role-playing program to open other components, such as creating bar status bar notification to, or start a service.

Second, the custom radio

In fact, custom broadcast the previous text has been talked about, we often dynamic registration is action we need to get through IntentFilter, in order to achieve dynamic registration, in fact, actionn it does not have to be a system-defined action (using the system defined in when it is appropriate behavioral change, access to the system's behavior and listening), of course, we can also customize (such as com.test.receiver), in addition to this change, in fact, standard and custom radio broadcast dynamic registration process without any difference.

Third, an orderly broadcast

Transmission is broadcast in order to customize the broadcast transmission method on the basis of changed, i.e. sendBroadcast (intent) to the sendOrderedBroadcast (intent, null), wherein the function is a second parameter to the string associated permissions . And set the priority priority.

<receiver
	android:name=".TestReceiver"
	android:enabled="true"
	android:exported="true">
	<Intent-filter android:prioirty="100">
		<action android:name=""/>
	</Intent-filter>
</reveiver>

So when sending broadcast, a result of receiving the same action, but due to a different set of priorities, it will be ordered state occurs. Resulting in orderly broadcast.

Fourth, the local broadcast

Previously transmitted are global broadcast system, a broadcast is issued back may be received by any other application, and may be received from any other broadcast applications, this can easily cause security problems, such as when carrying a number of critical data transmission broadcasting can be intercepted by other applications, or other applications to send spam broadcast to us.

The local broadcast value can be passed within the application, and broadcast only receive applications from broadcasting this issue, and this would resolve the security issues.

Use a local broadcast to broadcast LocalBroadcastManager to manage, and provides a method for sending broadcast and registration of.

public class MainActivity extends AppCompatActivity{
	private TestReceiver mTestReceiver;
	private TestReceiver mTestReceiver;
	private LocalBroadcastManager localBroadcastManager;
	
	@Override
	protected void onCreate(Bundle savedInstaceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mBtnSendBroadcast = findViewById(R.id.button);
		mTestReceiver = new TestReceiver();
		//LocalBroadcastManager的getInstance获取一个实例
		localBroadcastManager = LocalBroadcastManager.getInstance(this);
		mBtnSendBroadcast.setOnclickListener(new View.OnclickListener){
			@Override
			public void onClick(View v){
				Intent intent = new Intent("");
				localBroadcastManager.sendBroadcast(intent);
			}
		}
		IntentFilter filter = new IntentFilter();
		filter.addAction("");
		mTestReceiver = new TestReceiver();
		localBroadcastManager.regiseter(mTestReceiver, filter);
	}
	
	@Override
	protected void onDestroy(){
		localBroadcastManager.unregister(mTestReceiver);
		super.onDestroy();
	}
	
	class TestReceiver extends BroadCastReceiver{
		@Override
		public void onReceived(Context context, Intent intent){		

		}
	}
}

Content broadcast roughly divided into more than a few, in the actual development, usually used more dynamic registration, registration usually use dynamic broadcast to dynamically monitor changes in one state, so essential to monitor the use of flexible and dynamic.

Released two original articles · won praise 2 · views 53

Guess you like

Origin blog.csdn.net/qq_43139259/article/details/103932522