android获取系统wifi状态等

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android获取系统wifi状态等相关的知识,希望对你有一定的参考价值。

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获取系统wifi状态等的主要内容,如果未能解决你的问题,请参考以下文章

如何获取Android系统的wifi MAC和有线MAC

tasker获取wifi热点状态

Android 通过广播获取网络状态

android开发获取网络状态,wifi,wap,2g,3g.工具类

Android开发——WiFi信号检测

我的Android进阶之旅关于Android平台获取文件的mime类型:为啥不传小写后缀名就获取不到mimeType?为啥android 4.4系统获取不到webp格式的mimeType呢?(代码片段