,

How to check connection change from Wifi to Mobile data in Android using – java

Posted by

To check for a connection change from WiFi to mobile data in an Android application using Java, you can use the ConnectivityManager class. Here’s an example of how you can accomplish this:

  1. Add the necessary permissions to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  1. In your activity or fragment, add the following code to check for the connection change:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class YourActivity extends AppCompatActivity {

    private NetworkChangeReceiver networkChangeReceiver;

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

        // Create an instance of the BroadcastReceiver
        networkChangeReceiver = new NetworkChangeReceiver();

        // Register the BroadcastReceiver to receive network change events
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(networkChangeReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // Unregister the BroadcastReceiver when the activity is destroyed
        unregisterReceiver(networkChangeReceiver);
    }

    private class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Check the network connectivity status
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();

            if (activeNetwork != null && activeNetwork.isConnected()) {
                // Network is connected
                if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                    // WiFi connection
                    // Handle WiFi to mobile data transition
                    // ...
                } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                    // Mobile data connection
                    // Handle mobile data to WiFi transition
                    // ...
                }
            } else {
                // No network connection
            }
        }
    }
}

In this example, a NetworkChangeReceiver class is created by extending the BroadcastReceiver class. The onReceive method is overridden to handle the network change event. The ConnectivityManager is used to check the connectivity status and the type of the active network connection.

Inside the onReceive method, you can add your logic to handle the transition from WiFi to mobile data or vice versa based on the network type. You can perform specific actions or update your UI accordingly.

Remember to customize the code according to your specific application structure and requirements.