Skip to content

Commit

Permalink
Connectivity helper
Browse files Browse the repository at this point in the history
  • Loading branch information
laurynas committed Nov 13, 2011
1 parent 18029c9 commit 30252da
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/lt/appcamp/appcamp16/helpers/ConnectivityHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package lt.appcamp.appcamp16.helpers;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectivityHelper {

public static void requireNetwork(Context context) {
if (!ConnectivityHelper.isNetworkConnected(context)) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("You need to enable the network connection to use this application")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Context context = ((Dialog) dialog).getContext();
context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

builder.create().show();
}
}

public static boolean isNetworkConnected(Context context) {
return isWiFiNetworkConnected(context) || isMobileNetworkConnected(context);
}

public static boolean isWiFiNetworkConnected(Context context) {
return getWiFiNetworkInfo(context).isConnected();
}

private static NetworkInfo getWiFiNetworkInfo(Context context) {
return getConnectivityManager(context).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
}

public static boolean isMobileNetworkConnected(Context context) {
return getMobileNetworkInfo(context).isConnected();
}

private static NetworkInfo getMobileNetworkInfo(Context context) {
return getConnectivityManager(context).getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
}

private static ConnectivityManager getConnectivityManager(Context context) {
return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
}

0 comments on commit 30252da

Please sign in to comment.