1.发送标准广播
在发送广播之前,我们需要先定义一个广播接收器来准备接收此广播才行,不然发出去也是白发,
创建MyBroadcastReceiver.代码如下:
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
Toast.makeText(context,"received in MyBroadcastReceiver",Toast.LENGTH_SHORT).show();
}
}
这里当MyBroadcastReceiver收到自定义广播时,就会弹出”received in MyBroadcastReceiver”的提示
然后在AndroidManifest.xml中对这个广播接收器进行修改:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest">
...
<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=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android