Sunday, 11 December 2016

Android Wifi

Android WiFi manager can manage WiFi connectivity. And  it can be used as enable WiFi , disable WiFi and also add network or scanning for access points.

In Android manifest.XML we should add the "uses-permission". i.e shown below,

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> 

Activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:textAlignment="center">
 
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android Wifi " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Enable Wifi" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Disable Wifi" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button1" 
 android:layout_gravity="center_horizontal" />

</LinearLayout>


MainActivity.java :-

package com.kiranapp;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button enablebtn,disablebtn;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 enablebtn = (Button) findViewById(R.id.button);
 disablebtn = (Button) findViewById(R.id.button1);
 enablebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.setWifiEnabled(true);

 }
 });
 disablebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.setWifiEnabled(false);
 }
 });
 }
 }

Output :-

Android wifi disable mode
Android WiFi fig :1







             Android WiFi Disable mode












Android wifi enable mode
Android WiFi fig :2



                Android WiFi Enable mode

1 comment:

  1. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training in chennai | Android Training|Android Training in chennai with placement | Android Training in velachery

    ReplyDelete