Android Open Source - ShortcutsOfPower_Android My Net Controll






From Project

Back to project page ShortcutsOfPower_Android.

License

The source code is released under:

GNU General Public License

If you think the Android project ShortcutsOfPower_Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.networkprofiles.utils;
//from w  w w . j a v  a2s .c o  m
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.provider.Settings;

/** Provides methods for turning on/off the network interfaces **/
public class MyNetControll extends Object {

  //private AlarmManager am;
  private WifiManager wm;
  private BluetoothAdapter myBluetooth;
  private SharedPreferences prefs;
  private Context myContext;
  
  public MyNetControll(Context context) {
    myContext = context;
  }
  
  public void myStart() {
    //am = (AlarmManager) myContext.getSystemService(Context.ALARM_SERVICE);
    wm = (WifiManager) myContext.getSystemService(Context.WIFI_SERVICE);
    prefs = PreferenceManager.getDefaultSharedPreferences(myContext);
    myBluetooth = BluetoothAdapter.getDefaultAdapter();
  }
  
  public void wifiOn() {
    if (!wm.isWifiEnabled()) {
      wm.setWifiEnabled(true);
    }
  }
  public void wifiOff() {
    if (wm.isWifiEnabled()) {
      wm.setWifiEnabled(false);
    }
  }
  
  public void mobileOnOff() {
    Intent i = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
//    PendingIntent x = PendingIntent.getActivity(myContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
//    long triggerAtTime = System.currentTimeMillis();
//    am.set(AlarmManager.RTC, triggerAtTime, x);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    myContext.startActivity(i);
  }
  
  public void cellOff() {
      if (Settings.System.getInt(myContext.getContentResolver(),
              Settings.System.AIRPLANE_MODE_ON, 0) == 0) {
        prefs.edit().putBoolean("insaneState", true).commit();
        // Settings.System.putString(getContentResolver(),
        // Settings.System.AIRPLANE_MODE_RADIOS,
        // "cell, bluetooth, wifi");
        Settings.System.putString(myContext.getContentResolver(), Settings.System.AIRPLANE_MODE_RADIOS, "cell");
        
        Settings.System.putInt(myContext.getContentResolver(),
            Settings.System.AIRPLANE_MODE_ON, 1);
        Intent i = new Intent(
            Intent.ACTION_AIRPLANE_MODE_CHANGED);
        i.putExtra("state", true);
        myContext.sendBroadcast(i);
      }
  }
  
  public void cellOn() {
      if (Settings.System.getInt(myContext.getContentResolver(),
              Settings.System.AIRPLANE_MODE_ON, 0) == 1) {
        prefs.edit().putBoolean("insaneState", false).commit();
        Settings.System.putInt(myContext.getContentResolver(),
            Settings.System.AIRPLANE_MODE_ON, 0);
        Intent i = new Intent(
            Intent.ACTION_AIRPLANE_MODE_CHANGED);
        i.putExtra("state", false);
        myContext.sendBroadcast(i);
      }
  }
  
  public void bluetoothOn() {
    if (!myBluetooth.isEnabled()) {
      myBluetooth.enable();
    }
  }
  
  public void bluetoothOff() {
    if (myBluetooth.isEnabled()) {
      myBluetooth.disable();
    }
  }
  
  public void gpsOnOff() {
    //since API8 and still needs the WRITE_SECURE_SETTINGS permission as does 2. :(
    //1. Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
    //2. Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "");
    Intent i = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    myContext.startActivity(i);
//    PendingIntent x = PendingIntent.getActivity(myContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
//    long triggerAtTime = System.currentTimeMillis();
//    am.set(AlarmManager.RTC, triggerAtTime, x);
  }
  
  public void usbTetherOnOff() {
    Intent i = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    myContext.startActivity(i);
//    PendingIntent x = PendingIntent.getActivity(myContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
//    try {
//      x.send();
//    } catch (CanceledException e) {
//      e.printStackTrace();
//    }
  }
  
  public void displayOnOff() {
    Intent i = new Intent(android.provider.Settings.ACTION_DISPLAY_SETTINGS);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    myContext.startActivity(i);
//    PendingIntent x = PendingIntent.getActivity(myContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
//    try {
//      x.send();
//    } catch (CanceledException e) {
//      e.printStackTrace();
//    }
  }
  
  public void soundSettings() {
    Intent i = new Intent(android.provider.Settings.ACTION_SOUND_SETTINGS);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    myContext.startActivity(i);
  }
  
}




Java Source Code List

com.networkprofiles.MainNP.java
com.networkprofiles.SOPEula.java
com.networkprofiles.utils.ConstantsNP.java
com.networkprofiles.utils.MyNetControll.java
com.networkprofiles.utils.NPReceiver.java
com.networkprofiles.widget.NPWidgetCell.java
com.networkprofiles.widget.NPWidgetDisplay.java
com.networkprofiles.widget.NPWidgetGps.java
com.networkprofiles.widget.NPWidgetMobileData.java
com.networkprofiles.widget.NPWidgetService.java
com.networkprofiles.widget.NPWidgetSound.java