If
you want access device wifi programatically we need
- Permissions
- WifiManager
1.
In Project Manifest file add thease
permissons
<!-- we try to Enable or Disable WIFI, so we need permission
-->
<uses-permission
android:name="android.permission.CHANGE_WIFI_STATE"
/>
<!-- we try to fetching current WIFI state, so we need
permission -->
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"
/>
2.
In Project Activity file add this
code to get WifiManager
getting WIFI MANAGER from System Service
WifiManager wifiManager = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
methods
to access wifi status and change wifi status
fetching current WIFI status
wifiManager.isWifiEnabled()
change wifi status
wifiManager.setWifiEnabled(true or false);
enable wifi status
wifiManager.setWifiEnabled(true);
disable wifi status
wifiManager.setWifiEnabled(false);
-------------------------------------------------------------
Screen Shots:
-------------------------------------------------------------
1. MainActivity
public class
MainActivity extends Activity {
WifiManager wifiManager;
Button buttonON, buttonOFF;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// calling references or linking xml to java
textView = (TextView)
findViewById(R.id.textView);
buttonON = (Button) findViewById(R.id.buttonON);
buttonOFF = (Button) findViewById(R.id.buttonOFF);
// getting WIFI MANAGER from System Service
wifiManager = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
// fetching current WIFI status and update to TextView.
if (wifiManager.isWifiEnabled())
textView.setText("WIFI : ENABLED");
else
textView.setText("WIFI : DISABLED");
// listener for ON button
buttonON.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View v) {
// enable WIFI
wifiManager.setWifiEnabled(true);
textView.setText("WIFI : ENABLED");
}
});
// listener for OFF button
buttonOFF.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View v) {
// disable WIFI
wifiManager.setWifiEnabled(false);
textView.setText("WIFI : DISABLED");
}
});
}
}
2. 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:background="#800000ff"
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="com.venky.wifienabledemo.MainActivity"
>
<Button
android:id="@+id/buttonON"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="121dp"
android:text="ON"
android:textSize="15dp"
/>
<Button
android:id="@+id/buttonOFF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonON"
android:layout_below="@+id/buttonON"
android:layout_marginTop="76dp"
android:layout_weight="1"
android:text="OFF"
android:textSize="15dp"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="WIFI : "
android:textColor="#ffffff"
android:textSize="25dp"
android:textStyle="bold" />
</RelativeLayout>
3. AndroidManifest.xml
<?xml
version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.venky.wifienabledemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"
/>
<!-- we try to Enable or Disable WIFI, so we need permission
-->
<uses-permission
android:name="android.permission.CHANGE_WIFI_STATE"
/>
<!-- we try to fetching current WIFI state, so we need
permission -->
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name=".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>
************************************
************************************
No comments:
Post a Comment