Android广播Demo

具体步骤:

    1、初始化广播接收器BroadcastReceiver,接收到的广播会在该类的onReceive方法中回调

    2、注册广播registerReceiver(BroadcastReceiver,IntentFilter);

    3、发送广播sendBroadcast(Intent);


此demo是为了验证已经被覆盖的Activity在接收到广播时能否自己更新UI,下面是具体代码:

    第一步,创建MainActivity,并初始化广播接收器BroadcastReceiver、注册广播

public class MainActivity extends AppCompatActivity {
    private TextView tipTv;
    public static final String ACTION_NAME = "TAG_MAIN";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tipTv = (TextView) findViewById(R.id.tipTv);
        findViewById(R.id.gotoTv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(intent, 100);
                tipTv.setText("等待接收广播。。。");
            }
        });

        //注册广播
        registerBoradcastReceiver();
    }

    //广播接收器
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

        /**
         * 广播接收器,接收到广播的回调方法
         * @param context
         * @param intent
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("myp", "=== broadcast callback ===");
            String action = intent.getAction();
            if (action.equals(ACTION_NAME)) {
                //当接收到广播时,Toast会在最上层的Activity中提示,
                Toast.makeText(MainActivity.this, "broadcast callback", Toast.LENGTH_SHORT).show();
                String result = intent.getStringExtra("yaner");
                tipTv.setText(result);

                //当接收到广播时,Dialog会在接收到广播的界面提示,并且会随着该界面中的业务逻辑隐藏,
                // 由此可见,当Activity备覆盖式,如果又出发该Activity业务逻辑的操作,会在该Activity中正常执行
                initDialog();
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if (dialog != null && dialog.isShowing())
                            dialog.dismiss();
                    }
                }, 3 * 1000);
            }
        }
    };

    Dialog dialog;

    /**
     * 初始化弹出框
     */
    private void initDialog() {
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.view_dialog);
        dialog.show();
    }

    /**
     * 注册广播
     */
    public void registerBoradcastReceiver() {
        IntentFilter filer = new IntentFilter();//筛选条件
        filer.addAction(ACTION_NAME);//设置要注册的广播的标签
        //注册广播
        registerReceiver(mBroadcastReceiver, filer);
        Log.d("myp", "=== broadcast regist ===");
    }

}


    第二步,创建SecondActivity,并SecondActivity里实现触发 能发送广播到

   MainActivity里的广播的事件

public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        findViewById(R.id.brodcastBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(MainActivity.ACTION_NAME);
                mIntent.putExtra("yaner", "接收广播成功!!!");//接收到广播时,携带的数据

                //发送广播
                sendBroadcast(mIntent);
                Log.d("myp", "=== broadcast send ===");
            }
        });
    }
}

    

    xml布局文件比较简单,就不给出了,运行效果:

    1、点击按钮后,Toast提示在SecondActivity里直接提示

    2、点击按钮后,在3秒内返回MainActivity,Dialog正常显示,3秒过后消失

     3、点击按钮后,在3秒后返回MainActivity,Dialog已经消失


结论:被覆盖的Activity在接收到广播时能自己能更新UI

你可能感兴趣的:(Android广播)