一、主要Activityjava
package ecen.testwifi; import android.net.wifi.WifiManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; import android.content.Context; public class MainActivity extends Activity { private Button open; private Button close; private Button get; private WifiManager manager=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); open=(Button) findViewById(R.id.open); close=(Button) findViewById(R.id.close); get=(Button) findViewById(R.id.get); ButtonClick click=new ButtonClick(); open.setOnClickListener(click); close.setOnClickListener(click); get.setOnClickListener(click); } class ButtonClick implements OnClickListener{ @Override public void onClick(View view) { Button bu=(Button) view; int id=bu.getId(); manager=(WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE); if(R.id.open==id){ System.out.println("openWIFI"); manager.setWifiEnabled(true); }else if(R.id.close==id){ System.out.println("closeWIFI"); manager.setWifiEnabled(false); }else{ System.out.println("getWIFI"); System.out.println(manager.getWifiState()); } } } }
二、佈局文件android
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打開" /> <Button android:id="@+id/close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="關閉" /> <Button android:id="@+id/get" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獲取" /> </LinearLayout> </RelativeLayout>
三、AndroidManifest.xml文件app
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ecen.testwifi" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="ecen.testwifi.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> <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_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> </manifest>
這裏要特別注意的就是要在AndroidManifest這個文件當中要配置咱們對WIFI的操做權限ide