发送自定义广播:
类似于静态接收系统广播,不同的是要加一条发送广播命令,且在AndroidManifest中改自定义广播内容为要接收的广播。代码如下:
MainActivity.class:
Intent intent;Broadcast.class:
intent=new Intent("MyBroadcast");
sendBroadcast(intents);
import android.content.BroadcastReceiver;AdroidManifest.xml:
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
Toast.makeText(context,"MyBroadcast!",Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mole.test">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<use-permission android:name="android.permission.RECEIVER_BOOT_COMPLETED"/>
<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=".Broadcast"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="MyBroadcast"/>
</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>
类似于发送自定义广播,不同的是把发送广播的方法改为sendOrderBroadcast; 且在AndroidManifest中设置优先级,可以在接收器中阻断广播的继续传播。代码如下:
ActivityMain.class:
Intent intents=new Intent("MyBroadcast");Broadcast.class:
sendOrderedBroadcast(intents,null); //发送有序广播 void sendOrderedBroadcast(Intent intent,String receiverPermission)
import android.content.BroadcastReceiver;AndroidManifest.xml:
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
Toast.makeText(context,"MyBroadcast!",Toast.LENGTH_SHORT).show();
abortBroadcast(); //阻断广播的继续传播
}
}
<receiver
android:name=".Broadcast"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="MyBroadcast"/>
</intent-filter>
</receiver>
本地广播可以保护广播不被外部应用截获。先获取本地广播监听器实例,再给IntentFilter添加广播内容,然后注册广播管理器,编写一个继承于BroadcastReceiver的监听器类,里面编写收到广播后要执行的操作,
最后发送广播。代码如下:
ActivityMain.class:
LocalBroadcastManager localBroadcastManager;
Broadcast localReceiver;
localBroadcastManager=LocalBroadcastManager.getInstance(this);
localReceiver=new Broadcast();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("MyBroadcast");广播监听内容
localBroadcastManager.registerReceiver(localReceiver,intentFilter);//注册管理器
Intent localIntent;
localIntent= new Intent("MyLocalBroad");//广播发送内容
localBroadcastManager.sendBroadcast(localIntent);
class Broadcasts extends BroadcastReceiver {//监听器 @Override public void onReceive(Context context, Intent intent){ Toast.makeText(context,"我发送了一个本地广播",Toast.LENGTH_SHORT).show(); }}