Android 4.2:启用wifi直接时关闭wifi

时间:2013-07-23 23:54:40

标签: android wifi wifi-direct

对于android 4.2,如果需要不支持并发连接,并且从代码中我想在启用wifi direct后禁用WIFI。

我是否可以使用代码(在上一个问题中找到)执行此操作,例如:

WifiManager wifiManager  = (WifiManager)this.getSystemService(this.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);        //True - to enable WIFI connectivity .
                                         //False -disable WIFI connectivity.

2 个答案:

答案 0 :(得分:0)

试试此代码

Button btnWiFi = (Button)findViewById(R.id.btnWiFi);

        btnWiFi.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                WifiManager wifi =(WifiManager)getSystemService(Context.WIFI_SERVICE);
                if(wifi.isWifiEnabled())
                {
                    Toast.makeText(getBaseContext(), "WiFi is Enabled", Toast.LENGTH_LONG).show();

                }

                else
                {
                     AlertDialog builder = new AlertDialog.Builder(MainActivity.this).create();
                     builder.setTitle("Caution");
                     builder.setMessage("WiFi is Disabled."+"\n"+"Press Ok To Turn On WiFi.");
                     builder.setIcon(R.drawable.caution);
                     builder.setButton("OK", new DialogInterface.OnClickListener() {
                         public void onClick(final DialogInterface dialog, final int which) {
                         // Write your code here to execute after dialog closed
                             final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                             wifi.setWifiEnabled(true);


                         }
                 });

                     builder.setButton2("Cancel", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int which) {

                         }
                 });

                     builder.show();
                }


            }
        });

答案 1 :(得分:0)

这是不可能的!

当WIFI连接开启且所有设备都相同时,Wifi Direct仅查找对等体,连接和共享信息。