android wifi

<!-- 以下是使用wifi访问网络所需要的权限 -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

 

 

public class WifiActivity extends Activity {
    /** Called when the activity is first created. */
    private Button startButton = null;
    private Button stopButton = null;
    private Button checkButton = null;
    private WifiManager wifiManager = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startButton = (Button)findViewById(R.id.startWifi);
        stopButton = (Button)findViewById(R.id.stopWifi);
        checkButton = (Button)findViewById(R.id.checkWifi);
        startButton.setOnClickListener(new StartWifiListener());
        stopButton.setOnClickListener(new StopWifiListener());
        checkButton.setOnClickListener(new CheckWifiListener());
    }
    class StartWifiListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(true);
            System.out.println("wifi state --->"   wifiManager.getWifiState());
            Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为"   wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
        }
    }
    class StopWifiListener implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(false);
            System.out.println("wifi state --->"   wifiManager.getWifiState());
            Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为"   wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
        }
    
    }
    
    class CheckWifiListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
            System.out.println("wifi state --->"   wifiManager.getWifiState());
            Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为"   wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
        }
        
    }
}

 

 

image

 

 

 

image

posted on 2012-07-08 19:49  马德华  阅读(553)  评论(0编辑  收藏  举报