Guest User

Untitled

a guest
May 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. public class WifiReceiver extends BroadcastReceiver {
  2.  
  3. private static final String TAG = "WifiReceiver";
  4.  
  5. @Override
  6. public void onReceive(Context context, Intent intent) {
  7.  
  8. NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
  9.  
  10. if (networkInfo != null && networkInfo.isConnected()) {
  11. WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
  12. String ssid = wifiInfo.getSSID();
  13. Toast.makeText(context, "Conectado a: " + ssid, Toast.LENGTH_SHORT).show();
  14.  
  15. Log.i(TAG, "Connected to : " + ssid);
  16. }else{
  17. Log.e(TAG, "Network not connnected!");
  18. }
  19. }
  20. }
  21.  
  22. public class HelperConnection extends AppCompatActivity {
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_helper_connection);
  27.  
  28. this.setTitle("Login UCF");
  29.  
  30. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  31.  
  32. bindUI();
  33.  
  34. Toast.makeText(this, "Siga los pasos para establecer conexión con la red", Toast.LENGTH_LONG)
  35. .show();
  36.  
  37. btnNext.setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View view) {
  40. if (textInputUser.getText().length() == 0)
  41. textInputUser.setError("Campo requerido");
  42. else if (textInputPassw.getText().length() == 0)
  43. textInputPassw.setError("Campo requerido");
  44.  
  45. else {
  46. mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.in_from_left));
  47. mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.out_from_left));
  48.  
  49. mViewFlipper.showNext();
  50.  
  51. getSupportActionBar().setDisplayHomeAsUpEnabled(false);
  52.  
  53. progressBar.performClick();
  54.  
  55. if (wifiManager.isWifiEnabled()) {
  56. wifiManager.removeNetwork(wifiManager.getConnectionInfo().getNetworkId());
  57. detectWifi();
  58.  
  59. } else {
  60. wifiManager.setWifiEnabled(true);
  61. wifiManager.removeNetwork(wifiManager.getConnectionInfo().getNetworkId());
  62. detectWifi();
  63. }
  64. }
  65. }
  66. });
  67.  
  68. btnFinish.setOnClickListener(new View.OnClickListener() {
  69. @Override
  70. public void onClick(View view) {
  71. Intent intent = new Intent(HelperConnection.this, LoginActivity.class);
  72. intent.putExtra("runWeb", true);
  73. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  74. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  75. finish();
  76. startActivity(intent);
  77. }
  78. });
  79. }
  80.  
  81. private void bindUI() {
  82. mViewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
  83. mContext = this;
  84. btnFinish = (Button) findViewById(R.id.btnFinish);
  85. btnNext = (Button) findViewById(R.id.btnNext);
  86. progressBar = (ProgressBar) findViewById(R.id.load_steps_login);
  87. wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
  88. textInputPassw = (TextInputEditText) findViewById(R.id.edit_text_passw);
  89. textInputUser = (TextInputEditText) findViewById(R.id.edit_text_user);
  90. loginActivity = new LoginActivity();
  91. }
  92.  
  93. private void tryConnection() {
  94. loginActivity.helperConnect(wifiManager);
  95. Toast.makeText(this, "Espere...", Toast.LENGTH_SHORT).show();
  96.  
  97. IntentFilter intentFilter = new IntentFilter();
  98. intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
  99. WifiReceiver wifiReceiver = new WifiReceiver();
  100. registerReceiver(wifiReceiver, intentFilter);
  101. }
  102.  
  103. if (networkInfo != null && networkInfo.isConnected()) {
  104. WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
  105. String ssid = wifiInfo.getSSID();
  106. for(final WifiChangeListener listener : listeners) {
  107. listener.onWifiChange(ssid);
  108. }
  109. }
Add Comment
Please, Sign In to add comment