发送方
private LocalBroadcastManager localBroadcastManager;
……
localBroadcastManager = LocalBroadcastManager.getInstance(mContext);
//如果该类本身有界面的话直接用this没界面用context
Intent intent = new Intent("isnotTalking");
localBroadcastManager.sendBroadcast(intent);
接收方
private IntentFilter intentFilter;
private LocalBroadcastManager localBroadcastManager;
private LocalReceiver localReceiver;
……
//下面的写在onCreate()中
localBroadcastManager = LocalBroadcastManager.getInstance(this);
intentFilter = new IntentFilter();
intentFilter.addAction("isnotTalking");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,intentFilter);
//写在onDestroy()中
localBroadcastManager.unregisterReceiver(localReceiver);
class LocalReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context,Intent intent){
//这里写接收到广播后要执行的操作
}
}