Enable disable Wifi

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Enable disable Wifi

Postby avolovoy » Sun Dec 21, 2008 6:56 pm

Small tutorial just for that. Difficulty 0.5 out of 5.

First you need to permissions - to read Wifi state and to change it and here is manifest
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.      package="com.dynamix.mobile.wifi"
  6.  
  7.      android:versionCode="1"
  8.  
  9.      android:versionName="1.0.0">
  10.  
  11.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  12.  
  13.             <activity android:name="WifiToggle"
  14.  
  15.                  android:label="@string/app_name">
  16.  
  17.             <intent-filter>
  18.  
  19.                 <action android:name="android.intent.action.MAIN" />
  20.  
  21.                 <category android:name="android.intent.category.LAUNCHER" />
  22.  
  23.             </intent-filter>
  24.  
  25.         </activity>
  26.  
  27.     </application>
  28.  
  29. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
  30.  
  31. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
  32.  
  33.  
  34.  
  35. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4

when will use wifi manager to handle our operations to get the instance we'll do
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.       mWm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  3.  
  4.  
Parsed in 0.029 seconds, using GeSHi 1.0.8.4

And then will try to change settings by,
mWm.setWifiEnabled(false/true) to toggle wifi.
You can go all fancy to check the result by using getWifiState() to get better state of your wifi instead of isWifiEnabled()
full java code :



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class WifiToggle extends Activity implements
  3.  
  4.         CompoundButton.OnCheckedChangeListener {
  5.  
  6.  
  7.  
  8.     private CheckBox cb;
  9.  
  10.     private WifiManager mWm;
  11.  
  12.  
  13.  
  14.     /** Called when the activity is first created. */
  15.  
  16.     @Override
  17.  
  18.     public void onCreate(Bundle savedInstanceState) {
  19.  
  20.         super.onCreate(savedInstanceState);
  21.  
  22.         setContentView(R.layout.main);
  23.  
  24.  
  25.  
  26.         mWm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  27.  
  28.         LinearLayout mainView = (LinearLayout) findViewById(R.id.top);
  29.  
  30.         cb = (CheckBox) findViewById(R.id.check);
  31.  
  32.         cb.setOnCheckedChangeListener(this);
  33.  
  34.         if (mWm.isWifiEnabled()) {
  35.  
  36.             cb.setChecked(true);
  37.  
  38.             cb.setText("Wifi is : enabled");
  39.  
  40.         } else {
  41.  
  42.             cb.setChecked(false);
  43.  
  44.             cb.setText("Wifi is : disabled");
  45.  
  46.         }
  47.  
  48.  
  49.  
  50.     }
  51.  
  52.  
  53.  
  54.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  55.  
  56.    
  57.  
  58.         if (isChecked) {
  59.  
  60.             if (mWm.setWifiEnabled(true))
  61.  
  62.                 cb.setText("Wifi is : enabled");
  63.  
  64.             else {
  65.  
  66.                 cb.setChecked(false);
  67.  
  68.                 cb.setText("Attempt to enable failed");
  69.  
  70.             }
  71.  
  72.         } else {
  73.  
  74.             if (   (mWm.setWifiEnabled(false)))
  75.  
  76.                 cb.setText("Wifi is : disabled");
  77.  
  78.             else {
  79.  
  80.                 cb.setChecked(true);
  81.  
  82.                 cb.setText("Attempt to disable failed");
  83.  
  84.             }
  85.  
  86.         }
  87.  
  88.     }
  89.  
  90.  
  91.  
  92. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Layout is just a Linear layout with button inside.
Corrections or additions to the tut are welcome.
Alexey Volovoy
Bytesharp.NET
User avatar
avolovoy
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Mon Nov 03, 2008 10:32 pm
Location: kansas

Top

Postby amran » Tue Mar 17, 2009 11:15 am

Hi,
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. LinearLayout mainView = (LinearLayout) findViewById(R.id.top);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

what does this line do? Why must it be link to an ID=top.

I read that the android emualtor cant simulate wifi, so how do i test this program.

sorry im a noob:)
amran
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Mar 17, 2009 11:07 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests