Andriod learning: sending and receiving broadcasts, and andriod receiving broadcasts
1. Use standard Broadcast
1.1 define a broadcast Receiver
Public class MyBroadcastReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context arg0, Intent arg1 ){
// TODO Auto-generated method stub
String string = arg1.getStringExtra ("data ");
Toast. makeText (arg0, "received:" + string, Toast. LENGTH_SHORT). show ();
}
}
1.2 modify AndriodManifest. xml to register the broadcast Receiver
<Cycler android: name = ". MyBroadcastReceiver">
<Intent-filter>
<Action android: name = "com. example. broadcastreceiverdemo. BROADCAST"> </action>
</Intent-filter>
</Cycler>
1.3 add MainActivity code
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Button button = (Button) findViewById (R. id. btn );
Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
Intent intent = new Intent ("com. example. broadcastreceiverdemo. BROADCAST ");
Intent. putExtra ("data", "hello ");
SendBroadcast (intent );
}
});
}
}
2. Use local broadcast
Local broadcast can only be transmitted within the application, and the broadcast receiver can only receive broadcasts from the application, which improves the security of data transmission. However, local broadcasts cannot be received through static registration. Local broadcast uses LocalBroadcastManager to manage broadcasts, and provides methods for sending broadcasts and registering broadcast receivers.
// Define the broadcast Receiver
Public class LocalReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context arg0, Intent arg1 ){
// TODO Auto-generated method stub
String string = arg1.getStringExtra ("data ");
Toast. makeText (arg0, "received:" + string, Toast. LENGTH_SHORT). show ();
}
}
// MainActivity
Public class MainActivity extends Activity {
Private IntentFilter intentFilter;
Private LocalReceiver localReceiver;
Private LocalBroadcastManager localBroadcastManager;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
IntentFilter = new IntentFilter ("com. example. localbroadcastdemo. LOCALBROADCAST ");
LocalReceiver = new LocalReceiver ();
// Obtain the instance
LocalBroadcastManager = LocalBroadcastManager. getInstance (this );
// Register the local broadcast listener
LocalBroadcastManager. registerReceiver (localReceiver, intentFilter );
Button button = (Button) findViewById (R. id. btn );
Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
Intent intent = new Intent ("com. example. localbroadcastdemo. LOCALBROADCAST ");
Intent. putExtra ("data", "hello ");
// Send a local broadcast
LocalBroadcastManager. sendBroadcast (intent );
}
});
}
@ Override
Protected void onDestroy (){
// TODO Auto-generated method stub
Super. onDestroy ();
LocalBroadcastManager. unregisterReceiver (localReceiver );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
}