Test si le smartphone est connecté au réseau :
public class TestWifi {
public static boolean hasInternetConnection(Context ctx)
{
ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected())
{
return true;
}
/*
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnected())
{
return true;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected())
{
return true;
}
*/
return false;
}
}
Rajouter les permissions dans le fichier Manifest :
Test si le smartphone est connecté à un chargeur ou à un port USB :
public class PowerUtil {
public static boolean isPowered(Context context) {
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}
}
Code source ici
Références :
http://stackoverflow.com/questions/16577939/how-to-check-if-wifi-is-really-connected-in-android
http://stackoverflow.com/questions/5283491/check-if-device-is-plugged-in