Created
April 27, 2016 21:57
-
-
Save frederickbrock/4a09cc5d52f4fbb41fcef3ec4c87e760 to your computer and use it in GitHub Desktop.
networkconnectivity.java
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
public class PnNetworkMonitor { | |
private static PnNetworkMonitor instance; | |
private ConnectivityManager mConnectivityManager; | |
private Context mContext; | |
private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback(){ | |
@Override | |
public void onLost(Network network) { | |
Log.d(Constants.LOGT, "network has been lost"); | |
Intent connectionIntent = new Intent(); | |
connectionIntent.setAction("com.pubnub.android.examples.connectivity.ConnectionLost"); | |
connectionIntent.putExtra(Constants.NETWORK_CONNECTION_UPDATES, "ConnectionLost"); | |
mContext.sendBroadcast(connectionIntent); | |
} | |
@Override | |
public void onAvailable(Network network) { | |
super.onAvailable(network); | |
Intent connectionIntent = new Intent(Constants.NETWORK_CONNECTION_UPDATES); | |
connectionIntent.putExtra(Constants.NETWORK_CONNECTION_UPDATES, "ConnectionAvailable"); | |
mContext.sendBroadcast(connectionIntent); | |
} | |
@Override | |
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) { | |
Log.d(Constants.LOGT, "network link changed"); | |
Log.d(Constants.LOGT, network.toString()); | |
Intent connectionIntent = new Intent(Constants.NETWORK_CONNECTION_UPDATES); | |
connectionIntent.putExtra(Constants.NETWORK_CONNECTION_UPDATES, "ConnectionAvailable"); | |
mContext.sendBroadcast(connectionIntent); | |
} | |
}; | |
private PnNetworkMonitor(Context context) { | |
this.mContext = context; | |
this.mConnectivityManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); | |
this.mConnectivityManager.registerNetworkCallback(new NetworkRequest.Builder().build(), networkCallback); | |
} | |
public static PnNetworkMonitor getInstance(Context context) { | |
if(instance == null) { | |
Log.d(Constants.LOGT, "creating monitor instance"); | |
instance = new PnNetworkMonitor(context); | |
} | |
return instance; | |
} | |
public void start(){ | |
Log.d(Constants.LOGT, "starting monitor"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment