自定义广播发送者和接收者

public   class   MainActivity   extends   Activity {

       @Override
       protected   void   onCreate(Bundle savedInstanceState) {
               super .onCreate(savedInstanceState);
            setContentView(R.layout.   activity_main );
      }

       /**
       * 发送广播事件的方法
       *   @param   view
       */
      public   void   click(View  view){
      Intent intent= new   Intent();
      intent.setAction(   "com.itheima.broadcast.xxx" );    //设置意图动作
      sendBroadcast(intent);      //对外发送广播
      
      
    }



-----------------------------------------------------------------------------------------------------


   
          < receiver   android:name = "com.itheima.receiver.MyReceiver" >
              < intent-filter   >
                  < action   android:name = "com.itheima.broadcast.xxx"   />
                     intent-filter >
                   
                 receiver >





public   class   MyReceiver   extends   BroadcastReceiver {

       /**
       * 创建自定义广播接收者
       */
       @Override
       public   void   onReceive(Context context, Intent intent) {
            
          System.   out .println(   "我是自定义广播接收者我接收到了自定义广播事件....."   );
      }

}






你可能感兴趣的:(android基础笔记)