-6

is is possible to set own context after creating broadcastreceiver like this:?

public class MyFragment extends Fragment(){

Button myButton;

@Override
 onCreate {
  myButton = (Button) findview...
  myButton.setOnClickListner(myListener);
}
 .
 .
 .

MyListener {
@Override
OnClickListner {
  MyBroadCastReceiver receiver = new MyBroadCastReceiver()
  receiver.setContext(mContext)
  }
}    
    public static class MyBroadcastReceiver extends BroadcastReceiver {

     Context mContext;

     void setContext(Context context) {
      mContext = context;
     }

     @Override
     public void onReceive(Context context, Intent intent) {
      if (mContext!= null){
        log.d(TAG, "Context not null")
      }
    }

}

Every time method onReceive is invoked my mContext is null, is there any solution for that?

Nimdokai
  • 787
  • 1
  • 6
  • 18
  • can you please tell what you wanna do with that context? open an activity or something ? – Pratik Vyas May 30 '17 at 06:24
  • sure, I'd like to use DialogFragment, that's why I try to get context related to Activity. – Nimdokai May 30 '17 at 06:25
  • Please tell us why you need the activity context. Generally speaking, you can't assume the Activity still exists when the receiver is called, so you can't safely pass it an activity context. That's why its passed its own. The sole exception being when the receiver is registerd and unregistered within the activity, and then its usually an annonymous subclass that just access the activity as needed. – Gabe Sechan May 30 '17 at 06:26
  • Gabe Sechan thanks for replay, I forget to wrote it. MyBroadcastReceiver is inner class of Fragment, and it only exist (is registerd and unregistered) while this fragment exist. – Nimdokai May 30 '17 at 06:29
  • I agree with Gabe, but if you still want to handle this way then try passing value to Intent and you can open whatever you get their eg. if you want to open Mainactivity and its method pass it in intent, i know it is not good way but it may work – Pratik Vyas May 30 '17 at 06:30
  • @Nimdokai then you can use `getActivity()` but be sure on null checking. – Enzokie May 30 '17 at 06:30
  • @Enzokie I can't call getActivity() on context from receiver, only context.getApplicationContext() is available. – Nimdokai May 30 '17 at 07:05
  • @Nimdokai I mean `MyFragment.this.getActivity()` since your `BroadcastReceiver` is an inner class right? – Enzokie May 30 '17 at 07:09
  • @Enzokie right, but do you mean to use MyFragment.this.getActivity() in onReceive in BroadCastReceiver? – Nimdokai May 30 '17 at 07:22
  • @Nimdokai ok since your `BroadcastReceiver` is `static` which I thought *non static* at first then you cannot do that way. However you can put it in `receiver.setContext(MyFragment.this.getActivity())` – Enzokie May 30 '17 at 07:42

2 Answers2

0

try this my friend

public class MyReceiver extends BroadcastReceiver {
        Context mContext;

         public MyReceiver() {
        super();
       }

        @Override
        public void onReceive(Context context, Intent intent) {
            mContext = context;
            if (mContext != null) {
                log.d(TAG, "Context not null")
            }else{
            log.d(TAG, "  null Context ")
               }
        }
    }

create new object of your broadcast like this

MyReceiver receiver = new MyReceiver(this);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Hi, thanks for replay, but this will only change mContext to context from onReceive method parameter, I don't want this context, I really need Activity Context. – Nimdokai May 30 '17 at 06:21
  • than call your bradcast from activity and pass e.g MainActivity.this as context – AskNilesh May 30 '17 at 06:23
  • When I was trying to do so, I get messages that BroadCastReceiver must have default constructor. – Nimdokai May 30 '17 at 06:31
  • as I understand, you advise to create two constructors: 1. default, which call super() 2. with context as parameter right? – Nimdokai May 30 '17 at 06:39
  • yup bro @Nimdokai – AskNilesh May 30 '17 at 06:40
  • did it work for you...???? it is work in my case when i m trying with to check auto Internet connection checking. – AskNilesh May 30 '17 at 06:48
  • unfortunately not, i get RuntimeException: Unable to instantiate receiver com.mypackage.MyBroadCastReceiver java.lang.IllegalAccessException: void com.mypackage.MyBroadCastReceiver.() is not accessible from java.lang.Class – Nimdokai May 30 '17 at 07:03
  • please paste your whole code where you create broadcast object – AskNilesh May 30 '17 at 07:09
0

The problem was with declaring receiver in manifest, while it should be declared only in fragment, after changing it, everything works as it should. Thanks for help.

Nimdokai
  • 787
  • 1
  • 6
  • 18