Access to application class in Broadcast Receiver

前端 未结 2 704
慢半拍i
慢半拍i 2021-01-02 02:01

I want to check internet connection in Broadcast Receiver; And set result (A boolean flag) to a global variable, to use it on whole application, in if conditions; That if in

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-02 02:49

    You can access your Application class in BroadCastReceiver by using its context,

     @Override
     public void onReceive(final Context context, Intent intent) {
       MyApplication mApplication = ((MyApplication)context.getApplicationContext());
     }
    

提交回复
热议问题