Broadcast Intent 和Broadcast Receiver的使用

package tw.android;

import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {

    private Button mBtnRegReceiver,
                   mBtnUnregReceiver,
                   mBtnSendBroadcase1,
                   mBtnSendBroadcase2;

    private MyBroadcaseReceiver2 mMyReceiver2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setupViewComponent();
    }

    private void setupViewComponent() {
        mBtnRegReceiver = (Button)findViewById(R.id.btnRegReceiver);
        mBtnUnregReceiver = (Button)findViewById(R.id.btnUnregReceiver);
        mBtnSendBroadcase1 = (Button)findViewById(R.id.btnSendBroadcase1);
        mBtnSendBroadcase2 = (Button)findViewById(R.id.btnSendBroadcase2);
                   
        mBtnRegReceiver.setOnClickListener(btnRegReceiverOnClickLis);
        mBtnUnregReceiver.setOnClickListener(btnUnregReceiverOnClickLis);
        mBtnSendBroadcase1.setOnClickListener(btnSendBroadcase1OnClickLis);
        mBtnSendBroadcase2.setOnClickListener(btnSendBroadcase2OnClickLis);
    }

    private Button.OnClickListener btnRegReceiverOnClickLis = new Button.OnClickListener() {
        public void onClick(View v) {
            IntentFilter itFilter = new IntentFilter("tw.android.MY_BROADCAST2");
            mMyReceiver2 = new MyBroadcaseReceiver2();
            registerReceiver(mMyReceiver2, itFilter);
        }
    };

    private Button.OnClickListener btnUnregReceiverOnClickLis = new Button.OnClickListener() {
        public void onClick(View v) {
            unregisterReceiver(mMyReceiver2);    
         }
    };

    private Button.OnClickListener btnSendBroadcase1OnClickLis = new Button.OnClickListener() {
        public void onClick(View v) {
            Intent it = new Intent("tw.android.MY_BROADCAST1");
            it.putExtra("sender_name", "主程序");
            sendBroadcast(it);
        }
    };

    private Button.OnClickListener btnSendBroadcase2OnClickLis = new Button.OnClickListener() {
        public void onClick(View v) {
            Intent it = new Intent("tw.android.MY_BROADCAST2");
            it.putExtra("sender_name", "主程序");
            sendBroadcast(it);
        }
    };
}
package tw.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcaseReceiver1 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String sender = intent.getStringExtra("sender_name");
        Toast.makeText(context, "BroadcastReceiver1Μ收到�" + sender + "发送的Broadcase信息", 
                Toast.LENGTH_LONG).show();
    }

}
package tw.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcaseReceiver2 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String sender = intent.getStringExtra("sender_name");
        Toast.makeText(context, "BroadcastReceiver2Μ收到" + sender + "发送Broadcase的信息", 
                Toast.LENGTH_LONG).show();

    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    >
<Button android:id="@+id/btnRegReceiver"
       android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="注册Broadcase Receiver2"
    android:layout_marginTop="20dp"
    />
<Button android:id="@+id/btnUnregReceiver"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="注销Broadcase Receiver2"
    />
<Button android:id="@+id/btnSendBroadcase1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="传送MY_BROADCAST1"
    />
<Button android:id="@+id/btnSendBroadcase2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="传送MY_BROADCAST2"
    />
</LinearLayout>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="tw.android"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:targetSdkVersion="11" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".MyBroadcaseReceiver1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="tw.android.MY_BROADCAST1" />
            </intent-filter>
        </receiver>

    </application>
</manifest>
原文地址:https://www.cnblogs.com/zhoujn/p/4174614.html