How to detect Network change and change activity from A to B when Network is connected

Bir Nepali :

I have SplashActivity, Activity A and Activity B.

When the Internet is not available, Splash activity redirects to Activity A and when Internet is available and is connected SplashActivity redirects to Activity B.

I would like to immediately close the Activity A when user is connected itself and Open Activity B when user is still inside the app and he open his wifi or mobile Data.

Here is the code I'm using in SplahsActivity to redirect to Activity A and Activity B as per Network status

public static boolean isNetworkStatusAvialable(Context context) {
    ConnectivityManager cm =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null &&
            activeNetwork.isConnectedOrConnecting();

}

and I check with

  if (isNetworkStatusAvialable(getApplicationContext())) {
    // Load Activity B
  } else {
    Load Activity A and Toast Message, " No Internet"
  }

Thanks in advance.

Vikasdeep Singh :

In this case you should check network status using BroadcastReceiver because you immediately want to close current activity and move to other activity. So below is complete code for it:

ConnectivityStatusReceiver.java

public class ConnectivityStatusReceiver extends BroadcastReceiver {

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

    final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();

    if (activeNetworkInfo != null) {
      Toast.makeText(context, activeNetworkInfo.getTypeName() + " connected", Toast.LENGTH_SHORT).show();

      // Your code to start Activity B
      Activity activity = (Activity) context;
      intent = new Intent(activity, ActivityB.class);
      activity.startActivity(intent);

    } else {
      Toast.makeText(context, "No Internet or Network connection available", Toast.LENGTH_LONG).show();
    }
  }

}

MainActivity.java

public class MainActivity extends AppCompatActivity {
  ConnectivityStatusReceiver connectivityStatusReceiver;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    connectivityStatusReceiver = new ConnectivityStatusReceiver();
  }

  @Override
  protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(connectivityStatusReceiver, intentFilter);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if (connectivityStatusReceiver != null) {
      // unregister receiver
      unregisterReceiver(connectivityStatusReceiver);
    }
  }
}

To close Activity A you just add android:noHistory="true" in Manifest like below:

<activity android:label="@string/app_name" android:name="ActivityA"/>

Hope this will help you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104184&siteId=1