Android Disable and Enable WIFI, GORS and Mobile Data state useing Toggle Button?


Wifi_State.java

public class Wifi_State extends Activity {
private int bv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wifi);

bv = Build.VERSION.SDK_INT;

ToggleButton togglegps = (ToggleButton) findViewById(R.id.GpsButton);
togglegps
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {

if (isChecked) {
turnOnDataConnection(true, Wifi_State.this);
Toast.makeText(getApplicationContext(),
"gprs Enabled!", Toast.LENGTH_LONG).show();
} else {
turnOnDataConnection(false, Wifi_State.this);
Toast.makeText(getApplicationContext(),
"gprs Disabled!", Toast.LENGTH_LONG).show();
}
}
});

ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
toggleWiFi(true);
Toast.makeText(getApplicationContext(), "Wi-Fi Enabled!",
Toast.LENGTH_LONG).show();
} else {
toggleWiFi(false);
Toast.makeText(getApplicationContext(), "Wi-Fi Disabled!",
Toast.LENGTH_LONG).show();
}

if (isChecked) {
// Button is ON
Log.i("", "true");
Intent intent = new Intent(
"android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);

String provider = Settings.Secure.getString(
getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps")) { // if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
} else {
// Button is OFF
Log.i("", "false");
String provider = Settings.Secure.getString(
getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (provider.contains("gps")) { // if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
}
});
}

   // this is used for mobile data on and off condition

boolean turnOnDataConnection(boolean ON, Context context) {

try {
if (bv == Build.VERSION_CODES.FROYO)

{
Method dataConnSwitchmethod;
Class<?> telephonyManagerClass;
Object ITelephonyStub;
Class<?> ITelephonyClass;

TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);

telephonyManagerClass = Class.forName(telephonyManager
.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass
.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass()
.getName());

if (ON) {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("enableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("disableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);

} else {
final ConnectivityManager conman = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class<?> conmanClass = Class.forName(conman.getClass()
.getName());
final Field iConnectivityManagerField = conmanClass
.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField
.get(conman);
final Class<?> iConnectivityManagerClass = Class
.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass
.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
}
return true;
} catch (Exception e) {
Log.e("TAG", "error turning on/off data");
return false;
}
}

public void toggleWiFi(boolean status) {
WifiManager wifiManager = (WifiManager) this
.getSystemService(Context.WIFI_SERVICE);
if (status == true && !wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
} else if (status == false && wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}
}
}

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"
    tools:context=".MainActivity" >

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView1"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_marginBottom="37dp"
        android:text="ToggleButton" />
</RelativeLayout>

And give this permissions to You `Androidmanifest.xml`

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

No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...