AndroidManifest.xml
文件中声明)或者动态注册(在代码中注册)。onReceive(Context, Intent)
方法会被调用,该方法负责处理广播内容。AndroidManifest.xml
文件中声明 <receiver>
标签来完成。这种方式使得即使应用没有运行,也能够接收到特定的广播。Context.registerReceiver()
方法来注册广播接收器。这种方式通常在 Activity 或者 Service 中使用,并且需要在相应的生命周期方法中进行注册和注销操作。sendBroadcast()
, sendOrderedBroadcast()
, sendStickyBroadcast()
等方法发送广播。sendBroadcast()
) 会同时向所有匹配的接收者发送广播。sendOrderedBroadcast()
) 按照优先级顺序依次发送给接收者,前一个接收者可以修改广播的内容,甚至可以完全终止广播继续传播。sendStickyBroadcast()
) 在 Android 5.0 之后已被弃用,因为它会导致内存泄漏等问题。Intent
对象来携带信息的。Intent
可以包含动作(action)、数据(data)、类别(category)以及其他额外的数据。IntentFilter
来过滤想要监听的广播类型。onReceive()
方法执行期间有效。onReceive()
需要执行耗时操作,应该考虑将工作交给后台线程或者启动服务来处理。找到自己应用的包===>鼠标右键===>new ===> Other ===>Broadcast Receiver
输入名字===>点击finsh按钮 ==》等在新的类生成
这是类中的内容
package com.xiji.myapplicationttt; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. throw new UnsupportedOperationException("Not yet implemented"); } }
并且会在AndroidManifest.xml文件中静态注册,手动创建的广播接收者并不会自动静态注册
创建一个类,并且继承BroadcastReceiver
package com.xiji.myapplicationttt; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyDivBroadcastReceiver extends BroadcastReceiver { //重写接受方法 @Override public void onReceive(Context context, Intent intent) { System.out.println("MyDivBroadcastReceiver onReceive"); } }
package com.xiji.myapplicationttt; import android.content.IntentFilter; import android.os.Bundle; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class MainActivity extends AppCompatActivity { private MyDivBroadcastReceiver mDivReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); //定义广播接收者 mDivReceiver = new MyDivBroadcastReceiver(); String action = "myAction"; //广播过滤 IntentFilter filter = new IntentFilter(); filter.addAction(action); //注册 registerReceiver(mDivReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); //销毁注册 unregisterReceiver(mDivReceiver); } }
<receiver
android:name=".MyDivBroadcastReceiver"
android:enabled="true"
android:exported="true"></receiver>
完整的AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplicationttt" tools:targetApi="31"> <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true"></receiver> <receiver android:name=".MyDivBroadcastReceiver" android:enabled="true" android:exported="true"></receiver> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="广播发送者与接收者" android:layout_gravity="center" android:textSize="20dp" android:textColor="#DFECEF" android:background="#7BD113" android:gravity="center" android:padding="20dp" android:layout_marginBottom="30dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginBottom="20dp" android:padding="20dp" android:background="#3AD3E7" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#BBD2E9" android:gravity="center" android:text="消息推送站" android:textColor="#124578" android:textSize="20dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="20dp" android:padding="20dp" android:background="#DCF3F6" > <Button android:id="@+id/buttonSendMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/Theme.Design.NoActionBar" android:text="发送消息" /> <EditText android:layout_width="match_parent" android:layout_height="match_parent" android:hint="输入要发送的通知" android:inputType="text" android:id="@+id/editText" android:textColor="#456789" /> </LinearLayout> </LinearLayout> <!--定义三个广播接受者--> <!--第一个广播接受者--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="20dp" > <ImageView android:layout_width="128dp" android:layout_height="128dp" android:src="@drawable/a_girl" android:id="@+id/imageViewOne" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:id="@+id/firstTextView" android:text="等待接受消息" /> </LinearLayout> <!--第二个广播接受者--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="20dp" > <ImageView android:layout_width="128dp" android:layout_height="128dp" android:src="@drawable/girl_one" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:id="@+id/secondTextView" android:text="等待接受消息" /> </LinearLayout> <!--第三个广播接受则--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="128dp" android:layout_height="128dp" android:src="@drawable/boy_two" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:id="@+id/thirdTextView" android:text="等待接受消息" /> </LinearLayout> </LinearLayout>
package com.xiji.myapplicationttt; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class MainActivity extends AppCompatActivity { private MyDivBroadcastReceiver mDivReceiver; //定义三个接受者 private ReceiverMessage mReceiverMessage; private ReceiverMessageTwo mReceiverMessageTwo; private ReceiverMessageThree mReceiverMessageThree; //发送事件 private Button buttonSendMessage; //编辑消息 private EditText editTextMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); initListener(); initSubscriber(); } //订阅者初始化 private void initSubscriber() { //定义广播接收者 String action = "message"; mReceiverMessage = new ReceiverMessage(); mReceiverMessageTwo = new ReceiverMessageTwo(); mReceiverMessageThree = new ReceiverMessageThree(); //为每个接受者设置过滤器 IntentFilter filter = new IntentFilter(action); filter.addAction(action); IntentFilter filterTwo = new IntentFilter(action); filterTwo.addAction(action); IntentFilter filterThree = new IntentFilter(action); filterThree.addAction(action); //注册广播接收者 registerReceiver(mReceiverMessage, filter); registerReceiver(mReceiverMessageTwo, filterTwo); registerReceiver(mReceiverMessageThree, filterThree); //注册 } @Override protected void onDestroy() { super.onDestroy(); //销毁注册 unregisterReceiver(mReceiverMessage); unregisterReceiver(mReceiverMessageTwo); unregisterReceiver(mReceiverMessageThree); } //控件监听初始化 private void initListener() { buttonSendMessage = findViewById(R.id.buttonSendMessage); editTextMessage = findViewById(R.id.editText); buttonSendMessage.setOnClickListener(view -> { //发送消息 String message = editTextMessage.getText().toString(); Intent intent = new Intent(); intent.setAction("message"); intent.putExtra("myMessage", message); sendOrderedBroadcast(intent, null); }); } //延迟500ms private void delay() { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } //内部类 == >内部类可以调用外部类的成员 ==》获取控件 class ReceiverMessage extends BroadcastReceiver { //显示控件 private TextView firstTextView; @Override public void onReceive(Context context, Intent intent) { //通知信息 Log.i("ReceiverMessageOne", "收到广播信息" + intent.getStringExtra("myMessage").toString()); firstTextView = findViewById(R.id.firstTextView); //获取额外信息 firstTextView.setText("小久接受到消息:"+intent.getStringExtra("myMessage")); delay(); } } class ReceiverMessageTwo extends BroadcastReceiver { private TextView secondTextView; @Override public void onReceive(Context context, Intent intent) { Log.i("ReceiverMessageTwo", "收到广播信息" + intent.getStringExtra("myMessage")); secondTextView = findViewById(R.id.secondTextView); secondTextView.setText("张三接受到消息:"+intent.getStringExtra("myMessage").toString()); delay(); } } class ReceiverMessageThree extends BroadcastReceiver { private TextView thirdTextView; @Override public void onReceive(Context context, Intent intent) { Log.i("ReceiverMessageThree", "收到广播信息" + intent.getStringExtra("myMessage").toString()); thirdTextView = findViewById(R.id.thirdTextView); thirdTextView.setText("惜.己接受到消息:"+intent.getStringExtra("myMessage")); delay(); } } }
setPriority值越大优先级越高
filterTwo ===> filterThree ==>filter
//设置优先级 值越大优先级越高
filter.setPriority(100);
filterTwo.setPriority(300);
filterThree.setPriority(150);
package com.xiji.myapplicationttt; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class MainActivity extends AppCompatActivity { private MyDivBroadcastReceiver mDivReceiver; //定义三个接受者 private ReceiverMessage mReceiverMessage; private ReceiverMessageTwo mReceiverMessageTwo; private ReceiverMessageThree mReceiverMessageThree; //发送事件 private Button buttonSendMessage; //编辑消息 private EditText editTextMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); initListener(); initSubscriber(); } //订阅者初始化 private void initSubscriber() { //定义广播接收者 String action = "message"; mReceiverMessage = new ReceiverMessage(); mReceiverMessageTwo = new ReceiverMessageTwo(); mReceiverMessageThree = new ReceiverMessageThree(); //为每个接受者设置过滤器 IntentFilter filter = new IntentFilter(action); filter.addAction(action); IntentFilter filterTwo = new IntentFilter(action); filterTwo.addAction(action); IntentFilter filterThree = new IntentFilter(action); filterThree.addAction(action); //设置优先级 值越大优先级越高 filter.setPriority(100); filterTwo.setPriority(300); filterThree.setPriority(150); //注册广播接收者 registerReceiver(mReceiverMessage, filter); registerReceiver(mReceiverMessageTwo, filterTwo); registerReceiver(mReceiverMessageThree, filterThree); //注册 } @Override protected void onDestroy() { super.onDestroy(); //销毁注册 unregisterReceiver(mReceiverMessage); unregisterReceiver(mReceiverMessageTwo); unregisterReceiver(mReceiverMessageThree); } //控件监听初始化 private void initListener() { buttonSendMessage = findViewById(R.id.buttonSendMessage); editTextMessage = findViewById(R.id.editText); buttonSendMessage.setOnClickListener(view -> { //发送消息 String message = editTextMessage.getText().toString(); Intent intent = new Intent(); intent.setAction("message"); intent.putExtra("myMessage", message); sendOrderedBroadcast(intent, null); //无序推送 // sendBroadcast(intent); }); } //延迟500ms private void delay() { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } //内部类 == >内部类可以调用外部类的成员 ==》获取控件 class ReceiverMessage extends BroadcastReceiver { //显示控件 private TextView firstTextView; @Override public void onReceive(Context context, Intent intent) { //通知信息 Log.i("ReceiverMessageOne", "收到广播信息" + intent.getStringExtra("myMessage").toString()); firstTextView = findViewById(R.id.firstTextView); //获取额外信息 firstTextView.setText("小久接受到消息:"+intent.getStringExtra("myMessage")); delay(); } } class ReceiverMessageTwo extends BroadcastReceiver { private TextView secondTextView; @Override public void onReceive(Context context, Intent intent) { Log.i("ReceiverMessageTwo", "收到广播信息" + intent.getStringExtra("myMessage")); secondTextView = findViewById(R.id.secondTextView); secondTextView.setText("张三接受到消息:"+intent.getStringExtra("myMessage").toString()); delay(); } } class ReceiverMessageThree extends BroadcastReceiver { private TextView thirdTextView; @Override public void onReceive(Context context, Intent intent) { Log.i("ReceiverMessageThree", "收到广播信息" + intent.getStringExtra("myMessage").toString()); thirdTextView = findViewById(R.id.thirdTextView); thirdTextView.setText("惜.己接受到消息:"+intent.getStringExtra("myMessage")); delay(); } } }
2 ===> 3 ==> 1
abortBroadcast() //在onReceiver中添加
在三号ReceiverMessageThree添加拦截
class ReceiverMessageThree extends BroadcastReceiver { private TextView thirdTextView; @Override public void onReceive(Context context, Intent intent) { Log.i("ReceiverMessageThree", "收到广播信息" + intent.getStringExtra("myMessage").toString()); thirdTextView = findViewById(R.id.thirdTextView); thirdTextView.setText("惜.己接受到消息:"+intent.getStringExtra("myMessage")); //三号添加拦截 abortBroadcast(); delay(); } }
package com.xiji.myapplicationttt; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class MainActivity extends AppCompatActivity { private MyDivBroadcastReceiver mDivReceiver; //定义三个接受者 private ReceiverMessage mReceiverMessage; private ReceiverMessageTwo mReceiverMessageTwo; private ReceiverMessageThree mReceiverMessageThree; //发送事件 private Button buttonSendMessage; //编辑消息 private EditText editTextMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); initListener(); initSubscriber(); } //订阅者初始化 private void initSubscriber() { //定义广播接收者 String action = "message"; mReceiverMessage = new ReceiverMessage(); mReceiverMessageTwo = new ReceiverMessageTwo(); mReceiverMessageThree = new ReceiverMessageThree(); //为每个接受者设置过滤器 IntentFilter filter = new IntentFilter(action); filter.addAction(action); IntentFilter filterTwo = new IntentFilter(action); filterTwo.addAction(action); IntentFilter filterThree = new IntentFilter(action); filterThree.addAction(action); //设置优先级 值越大优先级越高 filter.setPriority(100); filterTwo.setPriority(300); filterThree.setPriority(150); //注册广播接收者 registerReceiver(mReceiverMessage, filter); registerReceiver(mReceiverMessageTwo, filterTwo); registerReceiver(mReceiverMessageThree, filterThree); //注册 } @Override protected void onDestroy() { super.onDestroy(); //销毁注册 unregisterReceiver(mReceiverMessage); unregisterReceiver(mReceiverMessageTwo); unregisterReceiver(mReceiverMessageThree); } //控件监听初始化 private void initListener() { buttonSendMessage = findViewById(R.id.buttonSendMessage); editTextMessage = findViewById(R.id.editText); buttonSendMessage.setOnClickListener(view -> { //发送消息 String message = editTextMessage.getText().toString(); Intent intent = new Intent(); intent.setAction("message"); intent.putExtra("myMessage", message); sendOrderedBroadcast(intent, null); //无序推送 // sendBroadcast(intent); }); } //延迟500ms private void delay() { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } //内部类 == >内部类可以调用外部类的成员 ==》获取控件 class ReceiverMessage extends BroadcastReceiver { //显示控件 private TextView firstTextView; @Override public void onReceive(Context context, Intent intent) { //通知信息 Log.i("ReceiverMessageOne", "收到广播信息" + intent.getStringExtra("myMessage").toString()); firstTextView = findViewById(R.id.firstTextView); //获取额外信息 firstTextView.setText("小久接受到消息:"+intent.getStringExtra("myMessage")); delay(); } } class ReceiverMessageTwo extends BroadcastReceiver { private TextView secondTextView; @Override public void onReceive(Context context, Intent intent) { Log.i("ReceiverMessageTwo", "收到广播信息" + intent.getStringExtra("myMessage")); secondTextView = findViewById(R.id.secondTextView); secondTextView.setText("张三接受到消息:"+intent.getStringExtra("myMessage").toString()); delay(); } } class ReceiverMessageThree extends BroadcastReceiver { private TextView thirdTextView; @Override public void onReceive(Context context, Intent intent) { Log.i("ReceiverMessageThree", "收到广播信息" + intent.getStringExtra("myMessage").toString()); thirdTextView = findViewById(R.id.thirdTextView); thirdTextView.setText("惜.己接受到消息:"+intent.getStringExtra("myMessage")); //三号添加拦截 abortBroadcast(); delay(); } } }
推送顺序还是2==》3===》1
可以看到消息在3的时候被拦截,没有向后传递
//无序推送
sendBroadcast(intent);
package com.xiji.myapplicationttt; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class MainActivity extends AppCompatActivity { private MyDivBroadcastReceiver mDivReceiver; //定义三个接受者 private ReceiverMessage mReceiverMessage; private ReceiverMessageTwo mReceiverMessageTwo; private ReceiverMessageThree mReceiverMessageThree; //发送事件 private Button buttonSendMessage; //编辑消息 private EditText editTextMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); initListener(); initSubscriber(); } //订阅者初始化 private void initSubscriber() { //定义广播接收者 String action = "message"; mReceiverMessage = new ReceiverMessage(); mReceiverMessageTwo = new ReceiverMessageTwo(); mReceiverMessageThree = new ReceiverMessageThree(); //为每个接受者设置过滤器 IntentFilter filter = new IntentFilter(action); filter.addAction(action); IntentFilter filterTwo = new IntentFilter(action); filterTwo.addAction(action); IntentFilter filterThree = new IntentFilter(action); filterThree.addAction(action); //设置优先级 值越大优先级越高 filter.setPriority(100); filterTwo.setPriority(300); filterThree.setPriority(150); //注册广播接收者 registerReceiver(mReceiverMessage, filter); registerReceiver(mReceiverMessageTwo, filterTwo); registerReceiver(mReceiverMessageThree, filterThree); //注册 } @Override protected void onDestroy() { super.onDestroy(); //销毁注册 unregisterReceiver(mReceiverMessage); unregisterReceiver(mReceiverMessageTwo); unregisterReceiver(mReceiverMessageThree); } //控件监听初始化 private void initListener() { buttonSendMessage = findViewById(R.id.buttonSendMessage); editTextMessage = findViewById(R.id.editText); buttonSendMessage.setOnClickListener(view -> { //发送消息 String message = editTextMessage.getText().toString(); Intent intent = new Intent(); intent.setAction("message"); intent.putExtra("myMessage", message); // sendOrderedBroadcast(intent, null); //无序推送 sendBroadcast(intent); }); } //延迟500ms private void delay() { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } //内部类 == >内部类可以调用外部类的成员 ==》获取控件 class ReceiverMessage extends BroadcastReceiver { //显示控件 private TextView firstTextView; @Override public void onReceive(Context context, Intent intent) { //通知信息 Log.i("ReceiverMessageOne", "收到广播信息" + intent.getStringExtra("myMessage").toString()); firstTextView = findViewById(R.id.firstTextView); //获取额外信息 firstTextView.setText("小久接受到消息:"+intent.getStringExtra("myMessage")); delay(); } } class ReceiverMessageTwo extends BroadcastReceiver { private TextView secondTextView; @Override public void onReceive(Context context, Intent intent) { Log.i("ReceiverMessageTwo", "收到广播信息" + intent.getStringExtra("myMessage")); secondTextView = findViewById(R.id.secondTextView); secondTextView.setText("张三接受到消息:"+intent.getStringExtra("myMessage").toString()); delay(); } } class ReceiverMessageThree extends BroadcastReceiver { private TextView thirdTextView; @Override public void onReceive(Context context, Intent intent) { Log.i("ReceiverMessageThree", "收到广播信息" + intent.getStringExtra("myMessage").toString()); thirdTextView = findViewById(R.id.thirdTextView); thirdTextView.setText("惜.己接受到消息:"+intent.getStringExtra("myMessage")); //三号添加拦截 abortBroadcast(); delay(); } } }
注:我们在代码中设置的有消息拦截,但是无序推送无法被拦截
无序推送无法被拦截,速度快,