//无序广播
btn_broad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送一个无序广播
Intent intent = new Intent();
intent.setAction("com.zxy1");
Bundle bundle = new Bundle();
bundle.putInt("msg",123);
intent.putExtras(bundle);
sendBroadcast(intent);//标准广播
}
});
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
//创建一个广播接受者
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
Log.i(TAG, "onReceive: ");
//频道
// an Intent broadcast.
String action = intent.getAction();
Bundle extras = intent.getExtras();
if (action.equals("com.zxy.broad")){
Log.i(TAG, "onReceive: "+extras.getString("name"));
}else if(action.equals("com.zxy1")){
Log.i(TAG, "onReceive: "+extras.getString("name"));
}
}
}
public class MyReceiver2 extends BroadcastReceiver {
private static final String TAG = "MyReceiver2";
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Bundle extras = intent.getExtras();
String name = extras.getString("name");
Log.i(TAG, "onReceive: "+name);
//判断是否是有序U广播
if (isOrderedBroadcast()){
abortBroadcast();//截断广播,终止
}
}
}
public class MyReceiver2 extends BroadcastReceiver {
private static final String TAG = "MyReceiver2";
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Bundle extras = intent.getExtras();
String name = extras.getString("name");
Log.i(TAG, "onReceive: "+name);
//判断是否是有序U广播
if (isOrderedBroadcast()){
abortBroadcast();//截断广播,终止
}
}
}
public class Main2Activity extends AppCompatActivity {
private MyReceiver myReceiver;
private Button btnm_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btnm_send=findViewById(R.id.btn_send);
//动态注册广播接受者
myReceiver= new MyReceiver();
//过滤器
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.zxy2");
registerReceiver(myReceiver,intentFilter);
//发送
btnm_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.zxy2");
sendBroadcast(intent);
}
});
}
//注销
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
}
public class Main3Activity extends AppCompatActivity {
private Button btn_send2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
//初始化控件
btn_send2=findViewById(R.id.btn_send2);
btn_send2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.zxy3");
sendBroadcast(intent);
}
});
}
}
public class MyReceiver3 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//发送通知
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("zxy").setContentText("nei");
// PendingIntent.getActivity(getActivity)
Notification build = builder.build();
//发送
manager.notify(1,build);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gao_1_0304_broad">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name=".MyReceiver3"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.zxy3"/>
</intent-filter>
</receiver>
<activity android:name=".Main3Activity" />
<activity android:name=".Main2Activity" />
<receiver
android:name=".MyReceiver2"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="900">
<action android:name="com.zxy1" />
</intent-filter>
</receiver>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="com.zxy.braod" />
<action android:name="com.zxy1" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
BroadCastReceiver
最新推荐文章于 2024-06-17 08:28:30 发布