码迷,mamicode.com
首页 > 移动开发 > 详细

Android获取各种设备的状态

时间:2015-04-30 12:10:18      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

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());

Android获取各种设备的状态

标签:

原文地址:http://www.cnblogs.com/Yellow0-0River/p/4468259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!