Created
March 11, 2014 17:53
-
-
Save snadjafi/9491244 to your computer and use it in GitHub Desktop.
internet connectivity
This file contains hidden or 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 MainActivity extends Activity implements ConnectivityDialogFragment.ConnectionDialogInterface { | |
| //region Variables | |
| private static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; | |
| private static final String DIALOG_FRAGMENT = "dialog_fragment"; | |
| private ConnectivityReceiver mConnectivityReceiver; | |
| private IntentFilter mFilter; | |
| //endregion | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| mConnectivityReceiver = new ConnectivityReceiver(); | |
| mFilter = new IntentFilter(CONNECTIVITY_ACTION); | |
| } | |
| @Override | |
| protected void onResume() { | |
| registerReceiver(mConnectivityReceiver, mFilter); | |
| super.onResume(); | |
| } | |
| @Override | |
| protected void onPause() { | |
| unregisterReceiver(mConnectivityReceiver); | |
| super.onPause(); | |
| } | |
| @Override | |
| public void onOkClick(DialogFragment dialogFragment) { | |
| if (dialogFragment != null) { | |
| dialogFragment.dismiss(); | |
| if (!isConnected()) { | |
| dialogFragment.show(getFragmentManager(),DIALOG_FRAGMENT); | |
| } | |
| } | |
| } | |
| private void showNetworkDialog() { | |
| ConnectivityDialogFragment fragment = (ConnectivityDialogFragment) getFragmentManager().findFragmentByTag(DIALOG_FRAGMENT); | |
| if (fragment == null) { | |
| fragment = new ConnectivityDialogFragment(); | |
| fragment.show(getFragmentManager(),DIALOG_FRAGMENT); | |
| fragment.setCancelable(false); | |
| } | |
| } | |
| private boolean isConnected() { | |
| ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); | |
| NetworkInfo netInfo = cm.getActiveNetworkInfo(); | |
| // return netInfo != null && netInfo.isConnectedOrConnecting(); | |
| return true; | |
| } | |
| private class ConnectivityReceiver extends BroadcastReceiver { | |
| public void onReceive(Context context, Intent intent) { | |
| if (!isConnected()) { | |
| showNetworkDialog(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment