Example usage for android.net.wifi WifiManager ACTION_PICK_WIFI_NETWORK

List of usage examples for android.net.wifi WifiManager ACTION_PICK_WIFI_NETWORK

Introduction

In this page you can find the example usage for android.net.wifi WifiManager ACTION_PICK_WIFI_NETWORK.

Prototype

String ACTION_PICK_WIFI_NETWORK

To view the source code for android.net.wifi WifiManager ACTION_PICK_WIFI_NETWORK.

Click Source Link

Document

Activity Action: Pick a Wi-Fi network to connect to.

Usage

From source file:com.nbplus.vbroadlauncher.BaseActivity.java

public void showNetworkConnectionAlertDialog(final boolean isFinishActivity) {
    new AlertDialog.Builder(this).setMessage(R.string.alert_network_message)
            //.setTitle(R.string.alert_network_title)
            .setCancelable(true)/*from w  w w .ja va2  s  . c om*/
            .setNegativeButton(R.string.alert_network_btn_check_wifi, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
                }
            }).setPositiveButton(R.string.alert_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    if (isFinishActivity) {
                        finish();
                    } else {
                        dialog.dismiss();
                    }
                }
            }).show();
}

From source file:ru.glesik.wifireminders.AddReminderActivity.java

protected void onResume() {
    super.onResume();
    // Creating adapter to populate spinnerSSID items.
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner Items = (Spinner) findViewById(R.id.spinnerSSID);
    Items.setAdapter(adapter);/*from  w  w w  .java 2 s  .  com*/
    WifiManager mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    // Start scanning for visible networks.
    if (mainWifi.isWifiEnabled()) {
        mainWifi.startScan();
        // Getting list of stored networks.
        List<WifiConfiguration> wifiList = mainWifi.getConfiguredNetworks();
        for (WifiConfiguration result : wifiList) {
            // Removing quotes.
            adapter.add(result.SSID.toString().replaceAll("^\"|\"$", ""));
        }
        adapter.notifyDataSetChanged();
    } else {
        new AlertDialog.Builder(this).setTitle(R.string.error_wifi_off_title)
                .setMessage(R.string.error_wifi_off_text)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
                        finish();
                    }
                }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setIcon(android.R.drawable.ic_dialog_alert).show();
    }
}

From source file:com.nbplus.vbroadlistener.BaseActivity.java

public void showNetworkConnectionAlertDialog() {
    new AlertDialog.Builder(this).setMessage(R.string.alert_network_message)
            //.setTitle(R.string.alert_network_title)
            .setCancelable(true)// w ww.j a  v  a  2  s. co  m
            .setNegativeButton(R.string.alert_network_btn_check_wifi, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
                }
            }).setPositiveButton(R.string.alert_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                }
            }).show();
}

From source file:prince.app.sphotos.Activity_Albums.java

public void checkConnection(View view) {
    startActivityForResult(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), CONNECT);
}

From source file:org.xbmc.android.remote.presentation.controller.AbstractController.java

public void onWrongConnectionState(int state, final INotifiableManager manager, final Command<?> source) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    switch (state) {
    case WifiHelper.WIFI_STATE_DISABLED:
        builder.setTitle("Wifi disabled");
        builder.setMessage("This host is Wifi only. Should I activate Wifi?");
        builder.setNeutralButton("Activate Wifi", new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                final ProgressDialog pd = new ProgressDialog(mActivity);
                pd.setCancelable(true);//from  w w  w  .j a  va2 s  . co m
                pd.setTitle("Activating Wifi");
                pd.setMessage("Please wait while Wifi is activated.");
                pd.show();
                (new Thread() {
                    public void run() {
                        final WifiHelper helper = WifiHelper.getInstance(mActivity);
                        helper.enableWifi(true);
                        int wait = 0;
                        while (wait <= MAX_WAIT_FOR_WIFI * 1000
                                && helper.getWifiState() != WifiHelper.WIFI_STATE_ENABLED) {
                            try {
                                sleep(500);
                                wait += 500;
                            } catch (InterruptedException e) {
                            }
                        }
                        manager.retryAll();
                        pd.cancel();
                        mDialogShowing = false;

                    }
                }).start();
            }
        });
        showDialog(builder);
        break;
    case WifiHelper.WIFI_STATE_ENABLED:
        final Host host = HostFactory.host;
        final WifiHelper helper = WifiHelper.getInstance(mActivity);
        final String msg;
        if (host != null && host.access_point != null && !host.access_point.equals("")) {
            helper.connect(host);
            msg = "Connecting to " + host.access_point + ". Please wait";
        } else {
            msg = "Waiting for Wifi to connect to your LAN.";
        }
        final ProgressDialog pd = new ProgressDialog(mActivity);
        pd.setCancelable(true);
        pd.setTitle("Connecting");
        pd.setMessage(msg);
        mWaitForWifi = new Thread() {
            public void run() {
                mDialogShowing = true;
                pd.show();
                (new Thread() {
                    public void run() {
                        int wait = 0;
                        while (wait <= MAX_WAIT_FOR_WIFI * 1000
                                && helper.getWifiState() != WifiHelper.WIFI_STATE_CONNECTED) {
                            try {
                                sleep(500);
                                wait += 500;
                            } catch (InterruptedException e) {
                            }
                        }
                        pd.cancel();
                        mDialogShowing = false;
                    }
                }).start();
                pd.setOnDismissListener(new OnDismissListener() {
                    public void onDismiss(DialogInterface dialog) {
                        if (helper.getWifiState() != WifiHelper.WIFI_STATE_CONNECTED) {
                            builder.setTitle("Wifi doesn't seem to connect");
                            builder.setMessage(
                                    "You can open the Wifi settings or wait " + MAX_WAIT_FOR_WIFI + " seconds");
                            builder.setNeutralButton("Wifi Settings", new OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    mDialogShowing = false;
                                    mActivity.startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
                                }
                            });
                            builder.setCancelable(true);
                            builder.setNegativeButton("Wait", new OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    mDialogShowing = false;
                                    mActivity.runOnUiThread(mWaitForWifi); //had to make the Thread a field because of this line
                                }
                            });

                            mActivity.runOnUiThread(new Runnable() {
                                public void run() {
                                    final AlertDialog alert = builder.create();
                                    try {
                                        if (!mDialogShowing) {
                                            alert.show();
                                            mDialogShowing = true;
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    }

                });
            }
        };
        mActivity.runOnUiThread(mWaitForWifi);
    }

}

From source file:de.j4velin.wifiAutoOff.Preferences.java

@SuppressWarnings("deprecation")
@Override//from w w  w .  ja v  a 2 s  . c o  m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

    if (BuildConfig.DEBUG && PermissionChecker.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED) {
        requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
    }

    status = (StatusPreference) findPreference("status");
    status.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(final Preference preference) {
            WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            boolean connected = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE))
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
            if (wm.isWifiEnabled() && !connected) {
                try {
                    startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                } catch (Exception e) {
                    Toast.makeText(Preferences.this, R.string.settings_not_found_, Toast.LENGTH_SHORT).show();
                }
            } else if (!wm.isWifiEnabled()) {
                wm.setWifiEnabled(true);
            } else {
                try {
                    startActivity(
                            new Intent(Settings.ACTION_WIFI_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                } catch (Exception e) {
                    Toast.makeText(Preferences.this, R.string.settings_not_found_, Toast.LENGTH_SHORT).show();
                }
            }
            return true;
        }
    });

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    final CheckBoxPreference screen_off = (CheckBoxPreference) findPreference("off_screen_off");
    screen_off.setSummary(
            getString(R.string.for_at_least, prefs.getInt("screen_off_timeout", Receiver.TIMEOUT_SCREEN_OFF)));

    if (!keepWiFiOn(this)) {
        screen_off.setChecked(false);
    }

    screen_off.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            if ((Boolean) newValue) {
                if (!keepWiFiOn(Preferences.this)) {
                    new AlertDialog.Builder(Preferences.this).setMessage(R.string.sleep_policy)
                            .setPositiveButton(R.string.adv_wifi_settings, new OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    try {
                                        startActivity(new Intent(Settings.ACTION_WIFI_IP_SETTINGS)
                                                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                                    } catch (Exception e) {
                                        Toast.makeText(Preferences.this, R.string.settings_not_found_,
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }).setNegativeButton(android.R.string.cancel, new OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).create().show();
                    return false;
                }
                if (android.os.Build.VERSION.SDK_INT >= 11) {
                    APILevel11Wrapper.showNumberPicker(Preferences.this, prefs, screen_off,
                            R.string.for_at_least, 1, 60, getString(R.string.minutes_before_turning_off_wifi_),
                            "screen_off_timeout", Receiver.TIMEOUT_SCREEN_OFF, false);
                } else {
                    showPre11NumberPicker(Preferences.this, prefs, screen_off, R.string.for_at_least, 1, 60,
                            getString(R.string.minutes_before_turning_off_wifi_), "screen_off_timeout",
                            Receiver.TIMEOUT_SCREEN_OFF, false);
                }
            }
            return true;
        }
    });

    findPreference("off_no_network").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            if ((Boolean) newValue) {
                if (android.os.Build.VERSION.SDK_INT >= 11) {
                    APILevel11Wrapper.showNumberPicker(Preferences.this, prefs, preference,
                            R.string.for_at_least, 1, 60, getString(R.string.minutes_before_turning_off_wifi_),
                            "no_network_timeout", Receiver.TIMEOUT_NO_NETWORK, false);
                } else {
                    showPre11NumberPicker(Preferences.this, prefs, preference, R.string.for_at_least, 1, 60,
                            getString(R.string.minutes_before_turning_off_wifi_), "no_network_timeout",
                            Receiver.TIMEOUT_NO_NETWORK, false);
                }
            }
            return true;
        }
    });

    final CheckBoxPreference on_at = (CheckBoxPreference) findPreference("on_at");
    on_at.setTitle(getString(R.string.at_summary, prefs.getString("on_at_time", Receiver.ON_AT_TIME)));
    on_at.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            if ((Boolean) newValue) {
                String[] time = prefs.getString("on_at_time", Receiver.ON_AT_TIME).split(":");
                final TimePickerDialog dialog = new TimePickerDialog(Preferences.this, new OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        prefs.edit().putString("on_at_time",
                                hourOfDay + ":" + (minute < 10 ? "0" + minute : minute)).commit();
                        on_at.setTitle(getString(R.string.at_summary,
                                hourOfDay + ":" + (minute < 10 ? "0" + minute : minute)));
                    }
                }, Integer.parseInt(time[0]), Integer.parseInt(time[1]), true);
                dialog.setOnCancelListener(new OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        on_at.setChecked(false);
                    }
                });
                dialog.setTitle(getString(R.string.turn_wifi_on_at_));
                dialog.show();
            }
            return true;
        }
    });

    final CheckBoxPreference off_at = (CheckBoxPreference) findPreference("off_at");
    off_at.setTitle(getString(R.string.at_summary, prefs.getString("off_at_time", Receiver.OFF_AT_TIME)));
    off_at.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            if ((Boolean) newValue) {
                String[] time = prefs.getString("off_at_time", Receiver.OFF_AT_TIME).split(":");
                final TimePickerDialog dialog = new TimePickerDialog(Preferences.this, new OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        prefs.edit().putString("off_at_time",
                                hourOfDay + ":" + (minute < 10 ? "0" + minute : minute)).commit();
                        off_at.setTitle(getString(R.string.at_summary,
                                hourOfDay + ":" + (minute < 10 ? "0" + minute : minute)));
                    }
                }, Integer.parseInt(time[0]), Integer.parseInt(time[1]), true);
                dialog.setOnCancelListener(new OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        off_at.setChecked(false);
                    }
                });
                dialog.setTitle(getString(R.string.turn_wifi_off_at_));
                dialog.show();
            }
            return true;
        }
    });

    final Preference on_every = findPreference("on_every");
    final String[] time_names = getResources().getStringArray(R.array.time_names);
    // default 2 hours
    on_every.setTitle(getString(R.string.every_summary, prefs.getString("on_every_str", time_names[4])));
    on_every.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            if ((Boolean) newValue) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Preferences.this);
                builder.setTitle(R.string.turn_wifi_on_every).setItems(time_names,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                prefs.edit().putInt("on_every_time_min", time_values[which])
                                        .putString("on_every_str", time_names[which]).commit();
                                on_every.setTitle(getString(R.string.every_summary, time_names[which]));
                            }
                        });
                builder.create().show();
            }
            return true;
        }
    });

    Preference locations = findPreference("locations");
    if (BuildConfig.FLAVOR.equals("play")) {
        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_NETWORK)) {
            locations.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(final Preference preference) {
                    startActivity(new Intent(Preferences.this, Locations.class));
                    return true;
                }
            });
        } else {
            locations.setEnabled(false);
        }
    } else {
        locations.setSummary("Not available in F-Droid version");
        locations.setEnabled(false);
    }

    final Preference power = findPreference("power_connected");
    power.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            if ((boolean) newValue) {
                Intent battery = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
                if (battery != null && battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) > 0) {
                    // already connected to external power
                    prefs.edit().putBoolean("ignore_screen_off", true).commit();
                }
            } else {
                prefs.edit().putBoolean("ignore_screen_off", false).commit();
            }
            return true;
        }
    });
}

From source file:nl.dobots.presence.PresenceDetectionApp.java

private void onNetworkError(String error, boolean present, String location, String additionalInfo) {

    // only trigger notification once as long as the network error is active.
    if (!_networkErrorActive) {
        _networkErrorActive = true;/*from w w  w.j ava 2 s. c  om*/

        if (_settings.isNotificationsEnabled()) {
            Intent contentIntent = new Intent(this, MainActivity.class);
            contentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent piContent = PendingIntent.getActivity(this, 0, contentIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            Intent wifiSettingsIntent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
            PendingIntent piWifiSettings = PendingIntent.getActivity(this, 0, wifiSettingsIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Network Error").setContentText(error)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(error))
                    .addAction(android.R.drawable.ic_menu_manage, "Wifi Settings", piWifiSettings)
                    .setContentIntent(piContent).setDefaults(Notification.DEFAULT_SOUND)
                    .setLights(Color.BLUE, 500, 1000);
            _notificationManager.notify(Config.PRESENCE_NOTIFICATION_ID, builder.build());
            Toast.makeText(this, error, Toast.LENGTH_LONG).show();
        }
    }

    // set logged in as false (because of network error)
    _ask.setLoggedIn(false);

    // store the presence update, will be triggered once network connection is reestablished
    _updateWaiting = true;
    _updateWaitingPresence = present;
    _updateWaitingLocation = location;
    _updateWaitingAdditionalInfo = additionalInfo;

    // just to make sure we don't get stuck
    _updatingPresence = false;
}

From source file:com.nbplus.vbroadlauncher.hybrid.BroadcastWebViewClient.java

public void showNetworkConnectionAlertDialog() {
    new AlertDialog.Builder(mContext).setMessage(R.string.alert_network_message)
            .setTitle(R.string.alert_network_title).setCancelable(true)
            .setNegativeButton(R.string.alert_network_btn_check_wifi, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    mContext.startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
                }//from   ww w .  jav a2s. c  o m
            }).setPositiveButton(R.string.alert_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                }
            }).show();
}

From source file:com.tml.sharethem.receiver.ReceiverActivity.java

private void promptToConnectManually(final String ssid) {
    showOptionsDialogWithListners(getString(R.string.p2p_receiver_oreo_msg, ssid),
            new DialogInterface.OnClickListener() {
                @Override/*from ww  w. j a v a2 s. co  m*/
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        Intent intent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
                        startActivity(intent);
                    } catch (Exception e) {
                        Toast.makeText(ReceiverActivity.this, "Wifi listings not found on your device!! :(",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.p2p_receiver_error_in_connecting, ssid), Toast.LENGTH_SHORT)
                            .show();
                    m_p2p_connection_status.setText(getString(R.string.p2p_receiver_scanning_hint));
                    startSenderScan();
                }
            }, "Settings", "Cancel");
}