自定义广播发送、接收

//发送自定义广播

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 发送广播事件 (消息)
* @param view
*/
public void click(View view){
Intent intent = new Intent();
//自定义一个广播动作。
intent.setAction("com.itheima.sender.jiuminga");
//大吼一声 把消息发出去了。 无序广播
sendBroadcast(intent);
//发送有序广播
//sendOrderedBroadcast(intent, receiverPermission);
}
}

//xml文件

<activity
android:name="com.itheima.sender.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

 

 

//广播接收

public class PoliceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "我是警察,我收到了消息", 1).show();
}

}

//xml文件

<receiver android:name="com.itheima.police.PoliceReceiver">
<intent-filter >
<action android:name="com.itheima.sender.jiuminga"/>
</intent-filter>
</receiver>

posted on 2015-06-21 20:37  寒星12345678999  阅读(298)  评论(0编辑  收藏  举报