Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android編程入門 >> android獲取系統wifi狀態等

android獲取系統wifi狀態等

編輯:Android編程入門

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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved