Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package test;
import android.arch.lifecycle.LiveData;
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 static test.InternetDetectionActivity.MobileData;
import static test.InternetDetectionActivity.WifiData;
/**
* Created by Saurabh(aqua) in 2017.
*/
public class ConnectionLiveData extends LiveData<ConnectionModel> {
private Context context;
public ConnectionLiveData(Context context) {
this.context = context;
}
@Override
protected void onActive() {
super.onActive();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(networkReceiver, filter);
}
@Override
protected void onInactive() {
super.onInactive();
context.unregisterReceiver(networkReceiver);
}
private BroadcastReceiver networkReceiver = new BroadcastReceiver() {
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getExtras()!=null) {
NetworkInfo activeNetwork = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
if(isConnected) {
switch (activeNetwork.getType()){
case ConnectivityManager.TYPE_WIFI:
postValue(new ConnectionModel(WifiData,true));
break;
case ConnectivityManager.TYPE_MOBILE:
postValue(new ConnectionModel(MobileData,true));
break;
}
} else {
postValue(new ConnectionModel(0,false));
}
}
}
};
}
package test;
import android.arch.lifecycle.LifecycleRegistry;
import android.arch.lifecycle.LifecycleRegistryOwner;
import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
/**
* Created by Saurabh(aqua) in 2017.
*/
public class InternetDetectionActivity extends AppCompatActivity implements LifecycleRegistryOwner {
public static final int MobileData = 2;
public static final int WifiData = 1;
private LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Live data object and setting an oberser on it */
ConnectionLiveData connectionLiveData = new ConnectionLiveData(getApplicationContext());
connectionLiveData.observe(this, new Observer<ConnectionModel>() {
@Override
public void onChanged(@Nullable ConnectionModel connection) {
/* every time connection state changes, we'll be notified and can perform action accordingly */
if (connection.getIsConnected()) {
switch (connection.getType()) {
case WifiData:
Toast.makeText(this, String.format("Wifi turned ON"), Toast.LENGTH_SHORT).show();
break;
case MobileData:
Toast.makeText(this, String.format("Mobile data turned ON"), Toast.LENGTH_SHORT).show();
break;
}
} else {
Toast.makeText(this, String.format("Connection turned OFF"), Toast.LENGTH_SHORT).show();
}
}
});
}
/* required to make activity life cycle owner */
@Override
public LifecycleRegistry getLifecycle() {
return lifecycleRegistry;
}
}
@karan4c6
Copy link

karan4c6 commented Feb 27, 2020

Kotlin Code for observing:

val connectionLiveData = ConnectionLiveData(applicationContext)
       connectionLiveData.observe(this, Observer {
           when (it?.isConnected) {
               true -> {
                   when (it.type) {
                       0 -> Toast.makeText(this, "Wifi turned on", Toast.LENGTH_SHORT).show()
                       1 -> Toast.makeText(this, "Mobile Data turned on", Toast.LENGTH_SHORT).show()
                   }
               }
               false -> {
                   Toast.makeText(this, "No Internet", Toast.LENGTH_SHORT).show()
               }

           }
       })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment