注意事项
示例代码(无序广播)
发送自动以广播:
public void sendBroadCast(View view) {
Intent intent = new Intent();
intent.putExtra("key", "哈哈哈我是一个无序广播");
intent.setAction("com.wl.lhw");
// com.example.www.app3 是要接收该广播的包 com.example.www.app3.MyReceiver 是要接收该广播的具体的类
intent.setComponent(new ComponentName("com.example.www.app3", "com.example.www.app3.MyReceiver"));
sendBroadcast(intent);
Toast.makeText(this, "广播发送成功", Toast.LENGTH_SHORT).show();
}
在另一个项目中接收广播
- AndroidManifest.xml接收器配置
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="com.wl.lhw"></action>
</intent-filter>
</receiver>
- 接收器代码
@Override
public void onReceive(Context context, Intent intent) {
String key = intent.getStringExtra("key");
Toast.makeText(context, "哈哈哈,我收到了广播" + key, Toast.LENGTH_LONG).show();
}