-
-
Save LionisIAm/cad0958fa73276492a537201755ddf2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment