简体   繁体   中英

How to use a receiver info at other activity or fragment, in Android?

I'm trying to make a receiver that will notify if and when the phone is connected to the Internet.

Now, the thing is that I have got a main activity that handles two fragments.
In one of the fragments, I have a search method that needs to use the Internet.
So, what I am trying to do is: if there's no Internet, then the user will get a toast, after he/she pressed the search button.

In order to do this - I've written the following class in the main activity -

public class ConnectionChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

       Boolean netBol;

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetInfo = connectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    boolean isConnected = activeNetInfo != null
        && activeNetInfo.isConnectedOrConnecting();

    if (isConnected){

         Boolean netBol = true;

    }else{

        netBol = false;
    Toast.makeText(context, R.string.no_internet, Toast.LENGTH_LONG).show();
    }
}

}

As you can see, I'm trying to use the Boolean netBol, to determine if there's a connection or not.

But the point is that i want to pass it to the search fragment, and I don't know how to do that.

I've also added the following line to the manifest:

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

and I also put the following code in the main activity;

   ConnectionChangeReceiver netReceiver = new ConnectionChangeReceiver();
            IntentFilter filterNet = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");

            registerReceiver(netReceiver, filterNet);

For my understanding this is saying that I've declared on a receiver, but I need one at the fragment.

So what I'm asking is: what should I do in order to pass the info from the receiver to the fragment, so that I could know if there's internet or not?

Simple. Use otto

You will find the sample application here .

Change your code as follows:

You can do something like this in your receiver:

if (isConnected){

BusProvider.getInstance().post(new InternetAvailable(true));

}else{

BusProvider.getInstance().post(new InternetAvailable(false));
Toast.makeText(context, R.string.no_internet, Toast.LENGTH_LONG).show();

}

InternetAvailable class can be like this:

public class InternetAvailable {
  private static boolean isInternetAvalable;


  public InternetAvailable(boolean isInternetAvailableParam) {
  isInternetAvailable = isInternetAvailableParam
  }

  @Produce public InternetAvailable isInternetAvailable() {
    return isInternetAvailable;

  }
}

The BusProvider class is like this:

public final class BusProvider {
  private static final Bus BUS = new Bus();

  public static Bus getInstance() {
    return BUS;
  }

  private BusProvider() {
    // No instances.
  }
}

Modify your SearchFragment like this:

@Override public void onResume() {
  super.onResume();
  BusProvider.getInstance().register(this);
}

@Override public void onPause() {
  super.onPause();
  BusProvider.getInstance().unregister(this);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM