测试工程师简历石沉大海?用接口自动化破局,拿到30%涨薪offer
2026/10/10 8:35:49
publicclassTestReceiverextendsBroadcastReceiver{publicstaticfinalStringTAG=TestReceiver.class.getSimpleName();@OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,"收到内容 - "+this);}}<receiverandroid:name=".mybroadcast.TestReceiver"android:exported="false"/><?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"tools:context=".MainActivity"><Buttonandroid:id="@+id/btn_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发送广播"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout>publicclassMainActivityextendsAppCompatActivity{publicstaticfinalStringTAG=MainActivity.class.getSimpleName();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButtonbtnSend=findViewById(R.id.btn_send);btnSend.setOnClickListener(v->{Intentintent=newIntent();ComponentNamecomponent=newComponentName(this,TestReceiver.class);intent.setComponent(component);sendBroadcast(intent);});}}收到内容 - com.my.broadcast.mybroadcast.TestReceiver@1a66a22收到内容 - com.my.broadcast.mybroadcast.TestReceiver@58d710fpublicclassTestReceiverextendsBroadcastReceiver{publicstaticfinalStringTAG=TestReceiver.class.getSimpleName();publicstaticfinalStringACTION=TAG;@OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,"收到内容 - "+this);}}<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"tools:context=".MainActivity"><Buttonandroid:id="@+id/btn_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发送广播"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout>publicclassMainActivityextendsAppCompatActivity{publicstaticfinalStringTAG=MainActivity.class.getSimpleName();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TestReceiverreceiver=newTestReceiver();IntentFilterintentFilter=newIntentFilter(TestReceiver.ACTION);registerReceiver(receiver,intentFilter);ButtonbtnSend=findViewById(R.id.btn_send);btnSend.setOnClickListener(v->{Intentintent=newIntent();intent.setAction(TestReceiver.ACTION);sendBroadcast(intent);});}}收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62publicclassTestReceiverextendsBroadcastReceiver{publicstaticfinalStringTAG=TestReceiver.class.getSimpleName();publicstaticfinalStringACTION=TAG;@OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,"收到内容 - "+this);}}publicclassMyApplicationextendsApplication{privateTestReceivertestReceiver;@OverridepublicvoidonCreate(){super.onCreate();testReceiver=newTestReceiver();IntentFilterfilter=newIntentFilter(TestReceiver.ACTION);registerReceiver(testReceiver,filter);}}<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"tools:context=".MainActivity"><Buttonandroid:id="@+id/btn_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发送广播"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout>publicclassMainActivityextendsAppCompatActivity{publicstaticfinalStringTAG=MainActivity.class.getSimpleName();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButtonbtnSend=findViewById(R.id.btn_send);btnSend.setOnClickListener(v->{Intentintent=newIntent();intent.setAction(TestReceiver.ACTION);sendBroadcast(intent);});}}收到内容 - com.my.broadcast.mybroadcast.TestReceiver@332c7bc收到内容 - com.my.broadcast.mybroadcast.TestReceiver@332c7bc如果在 Activity 中采用动态注册来注册广播接收器,需要在合适的时机注销广播接收器,否则会创建多个广播接收器实例
如果存在多个广播接收器实例,它们会同时接收广播
publicclassTestReceiverextendsBroadcastReceiver{publicstaticfinalStringTAG=TestReceiver.class.getSimpleName();publicstaticfinalStringACTION=TAG;@OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,"收到内容 - "+this);}}<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"tools:context=".MainActivity"><Buttonandroid:id="@+id/btn_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发送广播"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout>publicclassMainActivityextendsAppCompatActivity{publicstaticfinalStringTAG=MainActivity.class.getSimpleName();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);registerReceiver(newTestReceiver(),newIntentFilter(TestReceiver.ACTION));registerReceiver(newTestReceiver(),newIntentFilter(TestReceiver.ACTION));ButtonbtnSend=findViewById(R.id.btn_send);btnSend.setOnClickListener(v->{Intentintent=newIntent();intent.setAction(TestReceiver.ACTION);sendBroadcast(intent);});}}收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62 收到内容 - com.my.broadcast.mybroadcast.TestReceiver@be064f3收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62 收到内容 - com.my.broadcast.mybroadcast.TestReceiver@be064f3