In this tutorial we learn hoe to write code for turn on/off wifi using WifiManager in android example.Here not only turn on/off wifi,but get current state of wifi and change image according to state.
CREATE NEW ANDROID PROJECT
Step 1: Write code into activity_main.xml
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/hello_world" />
<ImageButton
android:id="@+id/onwifi"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@null"
android:src="@drawable/wifproc" />
<ImageButton
android:id="@+id/offwifi"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_alignTop="@+id/onwifi"
android:layout_marginLeft="18dp"
android:layout_toRightOf="@+id/onwifi"
android:contentDescription="@null"
android:src="@drawable/wifiisoff" />
</RelativeLayout>
Step 2: Write code into MainActivity.java
package dev.androidapplink.wifiapp;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton OnWifi;
ImageButton OffWifi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// wifi on off control
OnWifi = (ImageButton) findViewById(R.id.onwifi);
OffWifi = (ImageButton) findViewById(R.id.offwifi);
// call state change receiver
this.registerReceiver(this.WifiStateChangedReceiver, new IntentFilter(
WifiManager.WIFI_STATE_CHANGED_ACTION));
// set button click event
OnWifi.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// get wifi service
WifiManager wifiManager = (WifiManager) getBaseContext()
.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
}
});
// set button click event
OffWifi.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// get wifi service
WifiManager wifiManager = (WifiManager) getBaseContext()
.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}
});
}
// call state change receiver on button click
//set image according to wifi state
private BroadcastReceiver WifiStateChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int extraWifiState = intent.getIntExtra(
WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
switch (extraWifiState) {
case WifiManager.WIFI_STATE_DISABLED:
OnWifi.setImageResource(R.drawable.wifioff);
OffWifi.setImageResource(R.drawable.wifiison);
break;
case WifiManager.WIFI_STATE_DISABLING:
OffWifi.setImageResource(R.drawable.wifproc);
break;
case WifiManager.WIFI_STATE_ENABLED:
OnWifi.setImageResource(R.drawable.wifion);
OffWifi.setImageResource(R.drawable.wifiisoff);
break;
case WifiManager.WIFI_STATE_ENABLING:
OnWifi.setImageResource(R.drawable.wifproc);
break;
case WifiManager.WIFI_STATE_UNKNOWN:
break;
}
}
};
}
NOTE:Give permission in AndroidManifest.xml
Its allow to change wifi network state according to setting.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Step 3: Now Run Your Project:
CREATE NEW ANDROID PROJECT
Step 1: Write code into activity_main.xml
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/hello_world" />
<ImageButton
android:id="@+id/onwifi"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@null"
android:src="@drawable/wifproc" />
<ImageButton
android:id="@+id/offwifi"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_alignTop="@+id/onwifi"
android:layout_marginLeft="18dp"
android:layout_toRightOf="@+id/onwifi"
android:contentDescription="@null"
android:src="@drawable/wifiisoff" />
</RelativeLayout>
Step 2: Write code into MainActivity.java
package dev.androidapplink.wifiapp;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton OnWifi;
ImageButton OffWifi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// wifi on off control
OnWifi = (ImageButton) findViewById(R.id.onwifi);
OffWifi = (ImageButton) findViewById(R.id.offwifi);
// call state change receiver
this.registerReceiver(this.WifiStateChangedReceiver, new IntentFilter(
WifiManager.WIFI_STATE_CHANGED_ACTION));
// set button click event
OnWifi.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// get wifi service
WifiManager wifiManager = (WifiManager) getBaseContext()
.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
}
});
// set button click event
OffWifi.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// get wifi service
WifiManager wifiManager = (WifiManager) getBaseContext()
.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}
});
}
// call state change receiver on button click
//set image according to wifi state
private BroadcastReceiver WifiStateChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int extraWifiState = intent.getIntExtra(
WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
switch (extraWifiState) {
case WifiManager.WIFI_STATE_DISABLED:
OnWifi.setImageResource(R.drawable.wifioff);
OffWifi.setImageResource(R.drawable.wifiison);
break;
case WifiManager.WIFI_STATE_DISABLING:
OffWifi.setImageResource(R.drawable.wifproc);
break;
case WifiManager.WIFI_STATE_ENABLED:
OnWifi.setImageResource(R.drawable.wifion);
OffWifi.setImageResource(R.drawable.wifiisoff);
break;
case WifiManager.WIFI_STATE_ENABLING:
OnWifi.setImageResource(R.drawable.wifproc);
break;
case WifiManager.WIFI_STATE_UNKNOWN:
break;
}
}
};
}
NOTE:Give permission in AndroidManifest.xml
Its allow to change wifi network state according to setting.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Step 3: Now Run Your Project:
I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.
ReplyDeleteVery interesting blog.
seriallink.org
DU Meter Crack
IVT BlueSoleil Crack
WiFi Manager Crack
DiskTrix UltimateDefrag Crack