This article shares the usage method of Android learning Broadcast for your reference. The specific content is as follows
Implement broadcast of the startup prompt network
package ; import ; import ; import ; import ; import ; import ; import .; import ; import ; public class MainActivity extends AppCompatActivity { private IntentFilter intentFilter; private NetworkChangeReceiver networkChangeReceiver; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); intentFilter = new IntentFilter();//Create a filter instance (".CONNECTIVITY_CHANGE");//Add to receive CONNECTIVITY_CHANGE message networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver,intentFilter); } @Override protected void onDestroy() { (); unregisterReceiver(networkChangeReceiver); } class NetworkChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);//Get ConnectivityManager instance through this method NetworkInfo networkInfo = ();//Calling the getActiveNetworkInfo() method of the instance connectivityManager to get the NetworkInfo instance if (networkInfo != null && ()){ (context,"Network is available",Toast.LENGTH_SHORT).show(); }else { (context,"Network is unavailable",Toast.LENGTH_SHORT).show(); } } } }
Create BootCompleteReceiver class
Right-click, New->Other->Broadcast, enter the name BootCompleteReceiver, check Enable, Exported, and rewrite the onReceive() method. Since the class created by shortcuts is used, the required permissions are automatically registered in it. The tag is receiver, but it is not modified enough.
package ; import ; import ; import ; import ; public class BootCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { (context,"Boot Complete",Toast.LENGTH_SHORT).show(); } }
Register permission
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package=""> <uses-permission android:name=".ACCESS_NETWORK_STATE" />//Register and receive network message broadcast <uses-permission android:name=".RECEIVE_BOOT_COMPLETED"/>//Register and receive the power to start the broadcast <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="" /> <category android:name="" /> </intent-filter> </activity> <receiver android:name=".BootCompleteReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name=".BOOT_COMPLETED"/>//The system will send a broadcast when the power is turned on </intent-filter> </receiver> </application> </manifest>
The above-mentioned registration of received broadcast messages in the received broadcast is a static registration, and the received broadcasts registered in OnCreate() are a dynamic registration.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.