1.广播自定义活动A
点击按钮时发送自定义活动xcj.A
2.然后只需要进行监听即可
首先写一个继承了BroadcastReceiver的类响应监听
在AndroidManifest.xml中使用receiver标签监听自定义活动
AndroidManifest.xml文件如下
<?xml version="1.0" encoding="utf-8"?>
<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">
<activity android:name=".BActivity"
android:launchMode="standard">
</activity>
<activity android:name=".AActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ExampleAppWidgetProvider">
<intent-filter>
<action android:name="xcj.A"/>
</intent-filter>
</receiver>
</application>
receiver标签为注册监听部分,其中
<receiver android:name=".ExampleAppWidgetProvider">
相当于和监听用的类绑定。
而
<intent-filter>
<action android:name="xcj.A"/>
</intent-filter>
使该receiver只 响应 活动名为xcj.A的活动
当接收到 广播 xcj.A的活动 时,调用 ExampleAppWidgetProvider类中的OnReceive()方法进行响应