0

I am stuck in a problem. I have a broadcast receiver that calls the method of class takes in context and main activity reference in constructor. I dont know how to access main activity in broadcast receiver.Here is my code:

public void onReceive(@NonNull Context context, @NonNull Intent intent) {
        if (context == null) {
            throw new IllegalArgumentException();
        }
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();

        if (info != null) {
            if (info.isConnected()) { 
                if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                     Class myclass = new Class(context,mainactivity reference); //dont know how to get main activity here
                }

            }
        }
    }

Is there is any way that I can get it without Intent or there is some other method. I am in learning phase any help will be appreciated.

In manifest:

<receiver android:name="myreceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver> 

2 Answers2

0

I dont know how to access main activity in broadcast receiver

You don't. If this is a manifest-registered receiver, you may not have an instance of this activity, anyway.

Either:

  • Register the receiver from inside the Activity, via registerReceiver(), in which case your nested BroadcastReceiver class can reference the outer Activity instance, or

  • Use an event bus (greenrobot's EventBus, LocalBroadcastManager, etc.) to publish your event, and have your activity subscribe on that bus (if the activity happens to be around)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I could not understand your first line about registration. I have registered it in manifest see my edited code in the post – exceptionnotgood May 15 '17 at 14:56
  • @exceptionnotgood: Your process does not live forever. So, the user starts your app from the home screen launcher. The user then leaves your app. Your process is terminated. Then a connectivity change occurs. At this point, you receive the broadcast (at least on older Android devices), but you have no activity. A `BroadcastReceiver` registered in the manifest, as yours is, cannot make *any* assumptions about what activities may or may not exist in the process. – CommonsWare May 15 '17 at 15:03
  • thank you for you detailed explanation. So what should I do now? – exceptionnotgood May 15 '17 at 15:05
0
Try this hope it works:
in Main Activity:
public class MainActivity extends Activity {

public static MainActivity getInstance() {
    return new MainActivity();
}
}
And in your receiver:
MainActivity reference=MainActivity.getInstance();
Malika
  • 74
  • 3
  • The method you proposed will not in my case because in my case I am updating a textview in main activity from "Class" class and it will throw null pointer in that case – exceptionnotgood May 15 '17 at 15:29
  • then you have to give reference to main activity in on create method after inistialize ui..and use this reference in getInstance(). – Malika May 15 '17 at 15:41
  • hope it work: static MainActivity ref=null ; @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); //// Initialize ui ref=this; super.onCreate(savedInstanceState); } public static MainActivity getInstance() { return ref; } – Malika May 15 '17 at 16:15
  • Thank you very much. you solved my problem. Thank you – exceptionnotgood May 15 '17 at 16:55