Practical 20
Practical 20
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:layout_marginLeft="130dp"
android:text="Enable Wifi" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="130dp"
android:text="Disable Wifi" />
</LinearLayout>
AndroidManifest
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
MainActivity
package com.example.wifiexample;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
enableButton=(Button)findViewById(R.id.b1);
disableButton=(Button)findViewById(R.id.b2);
enableButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(MainActivity.this, "Wifi Enabled",
Toast.LENGTH_LONG).show();
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
}
});
disableButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(MainActivity.this, "Wifi Disabled",
Toast.LENGTH_LONG).show();
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);
}
});
}
}