In this application, we will learn how to enable and disable WiFi using simple code. So create new project and drop only one button on relative layout which will use to enable and disable WiFi and give id button1 to button. The code of android XML file is given below:
<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:background="#b21"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="wwf"
android:text="Wifi
On"
android:textSize="30sp"
/>
</RelativeLayout>
Now open your Java file and WiFi manager is used to get WiFi service and enable and disable WiFi. This is very simple because only isWiFienabled() method is used to check WiFi is on or off and change the state of WiFi. The code of android Java file is given below with explanation:
package
innosen.wwf; //your
package name
import
android.net.wifi.WifiManager;
import
android.os.Bundle;
import
android.app.Activity;
import
android.view.View;
import
android.widget.Button;
public
class
MainActivity extends
Activity {
WifiManager
wm;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//this
method will call on button click
public
void
wwf(View v)
{
Button
b1=(Button)findViewById(R.id.button1);
//get
Wifi
service
wm=(WifiManager)getSystemService(WIFI_SERVICE);
//Check
Wifi
is on or off
if(wm.isWifiEnabled())
{
b1.setText("Wifi
ON");
//enable
or disable Wifi
//for
enable pass true value
//for
disable pass false value
wm.setWifiEnabled(false);
}
else
{
b1.setText("Wifi
OFF");
wm.setWifiEnabled(true);
}
}
}
<?xml
version="1.0"
encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="selecm.wwf"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10"
/>
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:name="android.permission.CHANGE_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="innosen.wwf.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>
</manifest>
Now run your application and install .APK file in your mobile and test. You can’t test this application on emulator.
If you have any doubts please comment. Thanks… :)
please tell me where to use java code and xml code thank you
ReplyDeleteCreate new project in android eclipse and go to src folder -> package -> Main_Activity.java file and paste all Java code there and than go to res folder -> layout -> main_activity.xml file and paste XML code there...than change your AndroidManifast.xml file according to given above example.
DeleteHello Sir thanks for the help..
ReplyDeleteBut in case we want to again enable and diasble wifi on every click..how to perform that..as nw it jst respond to one click only..
just add one more button and use the second turn off service on that button click, so that's so easy.
DeleteIn Java:
wifiOn = (Button) findViewById(R.id.wifiOn);
wifiOff = (Button) findViewById(R.id.wifiOff);
final WifiManager wifiManager;
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
wifiOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
wifiManager.setWifiEnabled(true);
Toast.makeText(getApplicationContext(),"Wifi is Turned On",Toast.LENGTH_LONG).show();
}
});
wifiOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
wifiManager.setWifiEnabled(false);
Toast.makeText(getApplicationContext(),"Wifi is Turned Off",Toast.LENGTH_LONG).show();
}
});
sir, i want to connect more than one wifi at a time to share live streaming video from one android phone to more than one android phone can u help me source code sir
ReplyDeleteplz could u provide me with the source code of android attendance app using wifi
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletehello sir ... there is any method to turf wifi on through sms .. if it is posible then give me code thanks :)
ReplyDeleteActually it's possible. You'll have to take permission to read sms. Then make some special syntax to read with proper sync
Deletehow to find the .APK file to install and thank you. you're the best
ReplyDeletegetSystemService() marks an error in android studio
ReplyDeletejust write the complete code then error will be removed. or first add permission in Manifest file.
Deletesir.
ReplyDeleteHow to set download localation & Download file within our App.
sir.
ReplyDeleteHow to connect particular wifi ip address in our app?
Sir, can you please update the code to have a button that makes it cellular network data enabled,?
ReplyDelete