1.MainActivity2.java中的代码,主要是使用意图发送广播

public class MainActivity2 extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.receiver_demo);
	};
		
	
	public void click(View view){
		Intent intent=new Intent();
		intent.setAction("com.example.brocast.nimei");
		sendBroadcast(intent);
		
		
	}

 

2.在Manifest.xml过滤意图,然后设置BrocastReceiver所在的类文件

  <receiver android:name="com.example.brocastreceiverdemo.BrocastReceiverDemo2" >
            <intent-filter>
                <action android:name="com.example.brocast.nimei" />
            </intent-filter>
        </receiver>

 3、receiver.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="发送自定义广播" />

</LinearLayout>

 4. 自定广播的代码

public class BrocastReceiverDemo2 extends BroadcastReceiver{

	@Override
	public void onReceive(Context arg0, Intent arg1) {
		// TODO Auto-generated method stub
		Toast.makeText(arg0, "我是自定义的广播", 1).show();
	}

}