BroadcastReceiver broadcast receiver (1) -- register broadcast receiver using code

Source: Internet
Author: User

BroadcastReceiver broadcast receiver (1) -- register broadcast receiver using code

MainActivity is as follows:

Package cc. testbroadcasta; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. content. intentFilter;/*** Demo Description: * Register broadcast receiver in code * 1 register broadcast receiver in onCreate () * 2 register broadcast receiver in onDestroy () cancel registration of broadcast receiver */public class MainActivity extends Activity {private BroadcastReceiverTest mBroadcastReceiverTest; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (s AvedInstanceState); setContentView (R. layout. main); // register a broadcast receiver registerAReceiver (); // send a broadcast sendABroadcast ();} private void registerAReceiver () {mBroadcastReceiverTest = new BroadcastReceiverTest (); // set IntentFilterIntentFilter intentFilter = new IntentFilter (); intentFilter for the broadcast receiver. addAction (cc. test); registerReceiver (mBroadcastReceiverTest, intentFilter);} private void sendABroadcast () {Intent intent = new Inten T (); // sets Actionintent. setAction (cc. test); // carries data intent. putExtra (name, Stephen Chow); intent. putExtra (number, 9527); // send broadcast sendBroadcast (intent) ;}@ Overrideprotected void onDestroy () {super. onDestroy (); // cancel the registration of the broadcast receiver if (mBroadcastReceiverTest! = Null) {unregisterReceiver (mBroadcastReceiverTest );}}}

 

BroadcastReceiverTest is as follows:

 

Package cc. testbroadcasta; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; public class BroadcastReceiverTest extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {String name = intent. getStringExtra (name); int number = intent. getIntExtra (number, 0); System. out. println (received broadcast name = + name +, number = + number);} public static void main (String [] args ){}}

 

 

     
  
 


 

??

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.