android获取系统wifi状态等

转自:http://www.cnblogs.com/kulankadamei/archive/2013/04/24/3039835.html


WIFI

  获取WIFI状态

    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

    if(wifiManager != null){

      int wifiState = wifiManager.getWifiState();

    }

  附:Wifi状态有以下几种:(括号内为所对应的的int值)

  1. wifiManager.WIFI_STATE_DISABLED (1)

  2. wifiManager..WIFI_STATE_ENABLED (3)

  3. wifiManager..WIFI_STATE_DISABLING (0)

  4 wifiManager..WIFI_STATE_ENABLING  (2)

   设置wifi状态

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if(wifiManager == null){

      return;

    }

    int wifiApState = wifiManager.getWifiApState();  //获取wifi AP状态

    if((boolean值 && wifiApState == wifiManager.WIFI_AP_STATE_ENABLING || wifiApState == wifiManager.WIFI_AP_STATE_ENABLED){

      wifiManager.setWifiApEnabled(null,false);

    }

    wifiManager.setWifiEnabled(boolean值);

    俩处boolean值应相同

Bluetooth

蓝牙有待后续补充

 

GPS

  获取GPS状态

    ContentResolver resolver = context.ContextResolver();

    Boolean on = Settings.Secure.isLocationProviderEnabled(resolver, LocationManager.GPS_PROVIDER);

    true为开启状态

  设置GPS状态

    Settings..Secure.setLocationProviderEnabled(resolver,LocationManager.GPS_PROVIDER,boolean值);

    true为开启状态

SYNC(同步)

  获取当前系统同步状态

    ContentResolver resolver = context.ContextResolver();

    Boolean on = resolver.getMasterSyncAutomatically();

    ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

     设置同步

    ContentResolver.setMasterSyncAutomatically(boolean值);

 

AirplaneMode(飞行模式)

  获取系统当前飞行模式状态

    ContentResolver resolver = context.ContextResolver();

    boolean on = Settings.System.getInt(context.getContextResolver(),Settings.System.AIRPLANE_MODE_ON,0) == 1 ? true : false;

  设置飞行模式:

    Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON,boolean值 ? 1 : 0);

    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);

    intent.putExtra("state",boolean值);

    context.sendBroadcast(intent);

  俩个boolean值应该一致;

 

 

自动旋转屏幕

  获取系统当前自动旋屏设置

    ContentResolver resolver = context.ContextResolver();

    boolean on = Settings.System.getInt(context.getContResolver(),Settings.System.ACCELEROMETER_ROTATION) ==1 ? true : false;

  设置自动旋屏

    ContentResolver resolver = context.ContextResolver();

    Uri uri = Settings.System.getUriFor("accelerometer_rotation");

    Settings.System.putInt(resolver,"accelerometer_rotation",boolean值 ? 1 :0);

    resolver.notifyChange(uri,null);

 

静音模式

  获取静音模式

  public int getMuteMode(Context context){

    AudioManager audioManager = (AudioManager)context.getSystmService(Context.AUDIO_SERVICE);

    if(audioManager.getRingerMode == AudioManager.RINGER_MODE_SILENT){   //静音

      return 0;

    }else if(audioManager.getRingerMode == AudioManager.RINGER_MODE_VIBRATE){//振动

      return 1;

    } elsse {

      retrn 2;

    }

  }

  设置静音模式

  public void setMute(Context context){

     AudioManager audioManager = (AudioManager)context.getSystmService(Context.AUDIO_SERVICE);

    switch(getMuteMode(context){

      case 0:

        audioManager.stRingerMode(AudioManager.RINGER_MODE_VIBRATE); //振动模式

        break;

      case 1:

        audioManager.stRingerMode(AudioManager.RINGER_MODE_NORMAL); //正常模式

        break;

      case 2:

        audioManager.stRingerMode(AudioManager.RINGER_MODE_SILENT); //静音模式

        break;

      default:

        audioManager.stRingerMode(AudioManager.RINGER_MODE_NORMAL); //正常模式

    }

  }

 

 锁屏

  PowerManager pm = (PowerManager)context.getSystemService(context.POWER_SERVICE);

  pm.goToSleep(SystemClock.uptimeMillis());


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值