ConnectivityManager CONNECTIVITY_ACTION ( "android.net.conn.CONNECTIVITY_CHANGE" ). , ( ) .
, , , , .
public class InternetReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (isConnected())
Log.d("NetReceiver", "Internet is connected");
else
Log.d("NetReceiver", "Internet is not connected");
}
};
-, 3g
public boolean isConnected() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException e) { Log.e("ERROR", "IOException",e); }
catch (InterruptedException e) { Log.e("ERROR", "InterruptedException",e); }
return false;
}
:
<receiver android:name=".InternetReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>