从我的应用程序连接Wi-Fi

时间:2017-04-18 09:57:15

标签: android android-studio android-wifi

enter image description here

我正在开发开箱即用的体验应用程序。我必须连接    wi-fi来自我的自定义应用程序,不使用设备wifi    建立。        我已经完成了,从wifi管理器获取Wi-Fi网络列表。

请您指导我,如何从我的应用程序连接wifi?

listview.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long id) {
                    AlertDialog.Builder alert = new AlertDialog.Builder(WifiActivity.this);
                    str_wifi_nt_name = arg0.getItemAtPosition(position).toString();
                    alert.setTitle(str_wifi_nt_name);
                    LinearLayout layout = new LinearLayout(WifiActivity.this);
                    layout.setOrientation(LinearLayout.VERTICAL);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(20, 0, 30, 0);
                    wifi_password = new EditText(WifiActivity.this);
                    wifi_password.setHint("wi-fi password");
                    str_wifi_password=wifi_password.getText().toString();
                    layout.addView(wifi_password, params);
                    alert.setView(layout);
                    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            WifiConfiguration wifiConfig = new WifiConfiguration();
                            wifiConfig.SSID = String.format("\"%s\"", str_wifi_nt_name);
                            wifiConfig.preSharedKey = String.format("\"%s\"", str_wifi_password);
                        }
                    });
                    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        }
                    });
                    alert.show();
            }

        });
    }

如何设置wifi管理员的密码。请你帮我。

1 个答案:

答案 0 :(得分:1)

Intent resetIntent = new Intent("android.intent.action.MASTER_CLEAR");
                            if (getIntent().getBooleanExtra(SHUTDOWN_INTENT_EXTRA, false)) {
                             resetIntent.putExtra(SHUTDOWN_INTENT_EXTRA, true);
                                 }
                            sendBroadcast(resetIntent);