简单使用
步骤:
- 发送一条广播
Intent intent=new Intent("sunweihao");
sendBroadcast(intent);
- 接收广播:
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String ACTION1="sunweihao";
private static final String ACTION2="sunweihao2";
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION1==intent.getAction()) {
Toast.makeText(context, "孙伟豪", Toast.LENGTH_SHORT).show();
}else if (ACTION2==intent.getAction()) {
Toast.makeText(context, "孙伟豪2", Toast.LENGTH_SHORT).show();
}
}
}
- 注册广播
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="sunweihao"/>
<action android:name="sunweihao2"/>
</intent-filter>
</receiver>