目录
全局广播
默认发送的广播,就是全局广播,所有的App都可以请求接收
全局广播可以动态注册,也可以静态注册
<Button
android:text="静态广播"
android:onClick="stareceiver"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Button>
public void stareceiver(View view) {
Intent intent = new Intent();
intent.setAction("com.bw.sta");//设定动作
Bundle bundle = new Bundle();
bundle.putString("name","好好学");
bundle.putInt("age",21);
bundle.putFloat("money",22.2f);
intent.putExtras(bundle);
sendBroadcast(intent);//发送广播
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.bw.sta")){
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");
float money = bundle.getFloat("money");
Toast.makeText(context,"静态广播"+name+age+money,Toast.LENGTH_SHORT).show();
}
}
动态广播
需要用到registerReceiver
<Button
android:text="动态广播"
android:onClick="dyneceiver"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Button>
public void dyneceiver(View view) {
Intent intent = new Intent();
intent.setAction("com.bw.xx");
Bundle bundle = new Bundle();
bundle.putString("name","好好学");
bundle.putInt("age",21);
bundle.putFloat("money",22.4f);
intent.putExtras(bundle);
sendBroadcast(intent);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.bw.xx")){
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");
float money = bundle.getFloat("money");
Toast.makeText(context,"动态广播"+name+age+money,Toast.LENGTH_SHORT).show();
}
}
private OneReceiver oneReceiver;
private TwoReceiver twoReceiver;
private MyDynReceiver myDynReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDynReceiver = new MyDynReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.bw.xx");
registerReceiver(myDynReceiver,intentFilter);
}
本地广播
发送的广播事件不被其他运用程序获取,也不能响应其他运用程序发送的广播事件。
本地广播只能被动态注册,不能静态注册。动态注册或方法需要用到LocalBroadcastManager。
本地广播的优点:可以明确的知道正在发送的广播不会离开我们的程序,不用担心机密数据泄露,其他程序无法将广播发送到我们的程序内部,不需要担心安全问题,发送本地广播比系统全局效率高。
<Button
android:onClick="Local"
android:text="本地广播"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Button>
public void Local(View view) {
Intent intent = new Intent();
intent.setAction("com.bw.local");
localBroadcastManager.sendBroadcast(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyLocalReceiver myLocalReceiver =new MyLocalReceiver();
IntentFilter intentFilter =new IntentFilter();
intentFilter.addAction("com.bw.local");
localBroadcastManager=LocalBroadcastManager.getInstance(this);
localBroadcastManager.registerReceiver(myLocalReceiver,intentFilter);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.bw.local")){
Toast.makeText(context,"本地播放",Toast.LENGTH_SHORT).show();
}
}