Communication between BroadCast and Activity in Android, androidbroadcast

Source: Internet
Author: User

Communication between BroadCast and Activity in Android, androidbroadcast

Before reading this article, if you do not know much about the Broadcast Mechanism of Android, we recommend that you read a blog post I have posted: Illustration of the broadcast mechanism of Android.

Because this case is relatively simple, you can paste the code here without too much elaboration.

First effect:

The MainActivity code is as follows:
Package com. gc. testbroadcasedemo; import android. app. activity; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView;/***** @ author Android General **/public class MainActivity extends Activity {private Button mButton; private TextView mTextView; public static String ACTION_INTENT_TEST = "com. gc. broadcase. test "; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mTextView = (TextView) findViewById (R. id. message_ TV); mButton = (Button) findViewById (R. id. send_btn); mButton. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubIntent mIntent = new Intent (ACTION_INTENT_TEST ); sendBroadcast (mIntent) ;}}); disconnect () ;}// unbind from broadcast upon destruction @ Overrideprotected void onDestroy () {unregisterReceiver (mMessageReceiver); super. onDestroy ();} public MessageReceiver mMessageReceiver; public static String ACTION_INTENT_RECEIVER = "com. gc. broadcast. receiver ";/*** dynamically register broadcast */public void registerMessageReceiver () {mMessageReceiver = new MessageReceiver (); IntentFilter filter = new IntentFilter (); filter. addAction (ACTION_INTENT_RECEIVER); registerReceiver (mMessageReceiver, filter);} public class MessageReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {// TODO Auto-generated method stubif (intent. getAction (). equals (ACTION_INTENT_RECEIVER) {mTextView. setText (intent. getStringExtra ("message "));}}}}

The code for MyBroadCast is as follows:
Package com. gc. testbroadcasedemo; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. OS. bundle; import android. util. log;/***** @ author Android General **/public class MyBroadCast extends BroadcastReceiver {public MyBroadCast () {Log. v ("BROADCAST_TAG", "MyBroadCast") ;}@ Overridepublic void onReceive (Context context, Intent intent) {// TODO Auto-generated method stubLog. v ("BROADCAST_TAG", "onReceive"); Bundle bundle = intent. getExtras (); if (intent. getAction (). equals (MainActivity. ACTION_INTENT_TEST) {processCustomMessage (context, bundle) ;}// send msg to MainActivityprivate void processCustomMessage (Context context, Bundle bundle) {Intent mIntent = new Intent (MainActivity. ACTION_INTENT_RECEIVER); mIntent. putExtra ("message", "test communication between Broadcast and Activity"); context. sendBroadcast (mIntent );}}
The project directory is as follows:

Register the MyBroadCast broadcast in AndroidManifest. The Code is as follows:
 <receiver android:name=".MyBroadCast">            <intent-filter >                <action android:name="com.gc.broadcase.test"/>            </intent-filter>        </receiver>
Reprinted please indicate the source: http://blog.csdn.net/android_jiangjun/article/details/39928243


How does android activity respond to broadcast receive events?

Will handle be used? You will set the String you want to set to static. Every time you update it, handle will send a message to tell you that you want to update the activity.

How does Android implement continuous communication between Activity and Activity?

XXXService {public static final String ACTION_MESSAGE = "message"; onStartCommand (xxx) {// your logical xxx sendBroadcastReceiver (new IntentFilter (ACTION_MESSAGE ))}} XXXActivity {BroadcastReceiver extends ER = new BroadcastReceiver {onReceive (context, intent) {if (XXXService. ACTION_MESSAGE.equals (intent. getAction () {doSomething () ;}} onCreate () {// xxx registerReceiver (receiver, new IntentFilter (XXXService. ACTION_MESSAGE);} onPause () {unreigsterReceiver (caller);} public void doSomething () {xxx ;}}

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.