Andriod learning: sending and receiving broadcasts, and andriod receiving broadcasts

Source: Internet
Author: User

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;
}

}

 

Related Article

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.