控制WIFI状态

1.控制WIFI

public class MainActivity extends Activity {
    private Button startButton = null;
    private Button stopButton = null;
    private Button checkButton = null;
    private WifiManager wifiManager = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_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());
        /*if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }*/
    }
    class StartWifiListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            wifiManager = (WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(true);
            System.out.println("wifi state --->" + wifiManager.getWifiState());
            Toast.makeText(MainActivity.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)MainActivity.this.getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(false);
            System.out.println("wifi state --->" + wifiManager.getWifiState());
            Toast.makeText(MainActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
        }
    
    }
    
    class CheckWifiListener implements OnClickListener{

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

 

2.加入权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mars_1900_wifi"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mars_1900_wifi.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <!-- 以下是使用wifi访问网络所需要的权限 -->
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    
</manifest>

 

posted on 2014-04-07 15:19  上校  阅读(1913)  评论(0编辑  收藏  举报