BroadcastReceiver

private MyBroadcastReceiverB receiver;
onCreate()中
        receiver = new MyBroadcastReceiverB();//初始化广播接收器
        IntentFilter filter = new IntentFilter();//初始化意图过滤器
        filter.addAction("MYAction");//添加自己需要的意图
        getContext().registerReceiver(receiver, filter);//注册广播接收器
//内部类
public class MyBroadcastReceiverB extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //收到无序广播的名称
            String action = intent.getAction();
            //收到无序广播的内容
            String msg = intent.getStringExtra("msg");
            Log.i("zhujunxiana","00000"+action);
            Log.i("zhujunxiana","11111"+msg);
            //在此截断广播。广播的内容不会流向比这个优先级低的广播接收器。。。
            //abortBroadcast();
            txtUserName.setText(msg);
        }
    }
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                User user=userList.get(position);
                Intent intent = new Intent();
                intent.setAction("MYAction");
                intent.putExtra("msg",user.getName());
                sendBroadcast(intent);
                finish();
            }
        });

            
                
            

https://blog.csdn.net/chu_cheng/article/details/72720956
https://blog.csdn.net/weixin_44248652/article/details/87201012

你可能感兴趣的:(BroadcastReceiver)