android 广播的总结

一.广播的发送:

     //发送广播
        Intent intent = new Intent();
        //广播的标记
        intent.setAction("hello");
        //传递的信息.
        intent.putExtra("msg", "nihao");
        sendBroadcast(intent);

二.广播接受者静态注册,清单文件

1.定义一个类继承BroadCastReceiver,重写recevier方法

public class MyRecevier extends BroadcastReceiver {
    private static final String TAG = "MyRecevier";

    @Override
    public void onReceive(Context context, Intent intent) {

        System.out.println(intent.getAction() + "=====" + intent.getStringExtra("msg"));


    }
}

2.清单文件注册,其中intentFilter中的aciton可以是自定义发送广播的acttion内容,也可以是系统广播的action

       

        
            
                
            
        
    


三.动态注册广播,通过代码实现,优先级大于清单文件

    public void regresiter(View view) {
        IntentFilter filter = new IntentFilter();
        filter.addAction("hello");
        registerReceiver(mDemoReceiver, filter);

    }



    private class DemoReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println(intent.getAction() + "regresiter" + intent.getStringExtra("msg"));

        }


    }


你可能感兴趣的:(android,四大组件)