Hello friends,
Today we are going to learn about wifi and hotspot in android application.This is most use full topics in android. today we all are familer with this tool. using wifi we can share internet,files and do many more things. wifi and hotspot connecte togther and share internet in android.
So, first we need to provide some permission to do this features in android.
AndroidMainfest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/wifilauyout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<Button
android:id="@+id/btnwifion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="wifiOn"
android:text="WIFI ON" />
<Button
android:id="@+id/btnwifioff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btnwifion"
android:onClick="wifioff"
android:text="WIFI OFF" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/wifilauyout"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/btnhotspoton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="hotsportOn"
android:text="Hotspot ON" />
<Button
android:id="@+id/btnhotsportoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btnhotspoton"
android:onClick="hotsportoff"
android:text="Hotspot OFF" />
</RelativeLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
WifiManager wifi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifi = (WifiManager) MainActivity.this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
}
public void wifiOn(View view) {
if (!wifi.isWifiEnabled()) {
wifi.setWifiEnabled(true);
}
}
public void wifioff(View view) {
if (wifi.isWifiEnabled()) {
wifi.setWifiEnabled(false);
}
}
public void hotsportOn(View view) {
AppHotSpotManager apManager = new AppHotSpotManager(MainActivity.this);
apManager.turnWifiApOn();
}
public void hotsportoff(View view) {
AppHotSpotManager apManager = new AppHotSpotManager(MainActivity.this);
apManager.turnWifiApOff();
}
}
Output:
Today we are going to learn about wifi and hotspot in android application.This is most use full topics in android. today we all are familer with this tool. using wifi we can share internet,files and do many more things. wifi and hotspot connecte togther and share internet in android.
So, first we need to provide some permission to do this features in android.
AndroidMainfest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/wifilauyout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<Button
android:id="@+id/btnwifion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="wifiOn"
android:text="WIFI ON" />
<Button
android:id="@+id/btnwifioff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btnwifion"
android:onClick="wifioff"
android:text="WIFI OFF" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/wifilauyout"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/btnhotspoton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="hotsportOn"
android:text="Hotspot ON" />
<Button
android:id="@+id/btnhotsportoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btnhotspoton"
android:onClick="hotsportoff"
android:text="Hotspot OFF" />
</RelativeLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
WifiManager wifi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifi = (WifiManager) MainActivity.this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
}
public void wifiOn(View view) {
if (!wifi.isWifiEnabled()) {
wifi.setWifiEnabled(true);
}
}
public void wifioff(View view) {
if (wifi.isWifiEnabled()) {
wifi.setWifiEnabled(false);
}
}
public void hotsportOn(View view) {
AppHotSpotManager apManager = new AppHotSpotManager(MainActivity.this);
apManager.turnWifiApOn();
}
public void hotsportoff(View view) {
AppHotSpotManager apManager = new AppHotSpotManager(MainActivity.this);
apManager.turnWifiApOff();
}
}
Output:
Comments
Post a Comment