广播:发送有序、无序(标准)广播
2019-07-03 本文已影响3人
穿越平行宇宙
image.png
一、创建3个广播接收器类,先静态注册
enabled 启用
exported 出口,输出
priority 优先级
<receiver
android:name=".type2.service.MyReceiver1"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="80">
<action android:name="com.jiyun.action.ABC" />
</intent-filter>
</receiver>
<receiver
android:name=".type2.service.MyReceiver2"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="90">
<action android:name="com.jiyun.action.ABC" />
</intent-filter>
</receiver>
<receiver
android:name=".type2.service.MyReceiver3"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.jiyun.action.ABC" />
</intent-filter>
</receiver>
二、写布局
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".type2.activity.Main2Activity">
<Button
android:id="@+id/sendBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标准" />
<Button
android:id="@+id/sendOrderBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="有序" />
</LinearLayout>
三、activity 包下的
MainActivity2.java
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
/**
* 标准
*/
private Button mSendBtn;
/**
* 有序
*/
private Button mSendOrderBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
}
private void initView() {
mSendBtn = (Button) findViewById(R.id.sendBtn);
mSendBtn.setOnClickListener(this);
mSendOrderBtn = (Button) findViewById(R.id.sendOrderBtn);
mSendOrderBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.sendBtn:
// 定义action 活动,发送标准广播
Intent intent = new Intent("com.jiyun.action.ABC");
intent.putExtra("name", "guolei");
sendBroadcast(intent);
break;
case R.id.sendOrderBtn:
// 发送有序广播
Intent intent1 = new Intent("com.jiyun.action.ABC");
sendOrderedBroadcast(intent1, null);
break;
}
}
}
四、service 包下的
1. MyReceiver1.java:接收到传输的消息,并toast,然后跳转到MainActivity页面
public class MyReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收广播发送的消息
String name = intent.getStringExtra("name");
Toast.makeText(context, "MyReceiver1" + name, Toast.LENGTH_SHORT).show();
// 跳转到第一个页面
Intent intentMain = new Intent(context, MainActivity.class);
intentMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//非页面之外启动页面,需要自带加载任务栈
context.startActivity(intentMain);
}
}
2. MyReceiver2.java:toast,并且拦截广播
public class MyReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"MyReceiver2",Toast.LENGTH_SHORT).show();
abortBroadcast();//拦截广播
}
}
3. MyReceiver3.java:toast
public class MyReceiver3 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"MyReceiver3",Toast.LENGTH_SHORT).show();
}
}