Android实现广播机制

直接上代码

//把他当一个变量定义在Android Activity类中或自己代码方便的位置
 BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
           //你的操作
        }
    };

需要接受广播的位置进行注册广播

 public static final String action = "ble.over.action";
 
 IntentFilter filter = new IntentFilter(MainActivity.action);
 registerReceiver(broadcastReceiver, filter);

最后在需要发送广播的位置进行调用

Intent intent = new Intent(action);
intent.putExtra("type", "one");//intent可以传递参数就不多说了
context.sendBroadcast(intent);

写的请多指教

你可能感兴趣的:(Android实现广播机制)