Why is my BroadCast Receiver not working?

前端 未结 6 994
礼貌的吻别
礼貌的吻别 2021-01-21 16:03

I am learning about BroadCast Receivers. What I am trying to achieve with the following piece of code is, I would like to see a toast when I switch to airplane mode, where the a

6条回答
  •  感动是毒
    2021-01-21 16:57

    Try it like this,try adding,android:exported="true":

    //Add this permission too

    
    
    
     
            
                
            
        
    
    The Real state is determined like this:
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
            if(isAirplaneModeOn){
    
               // handle Airplane Mode on
            } else {
               // handle Airplane Mode off
            }
        }
    

提交回复
热议问题