1. 可以跨应用传递消息,也可以在本应用中传递数据
1. 发送广播
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);
}
}
2. 接收者
配置文件
<receiver android:name="com.itheima.police.PoliceReceiver"> <span style="font-family: Arial, Helvetica, sans-serif;"><intent-filter> <action android:name="com.itheima.sender.jiuminga"/> </intent-filter> </receiver></span>
处理
public class PoliceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "我是警察,我收到了消息", 1).show();
}
}