首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android --如果手机是在飞机模式下还是不在网络中,我该如何获得它?

android --如果手机是在飞机模式下还是不在网络中,我该如何获得它?
EN

Stack Overflow用户
提问于 2017-07-28 19:24:44
回答 2查看 886关注 0票数 2

我想知道信号电平。为此,我使用以下方法。但是我不知道如何联系电话,如果它是在飞机模式或停止服务。你能帮帮我吗?

代码语言:javascript
复制
public void signalLevel() {
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    List<CellInfo> all = tm.getAllCellInfo();
    String a = all.get(0).getClass().getName();


    if (a.equals("android.telephony.CellInfoLte")) {
        CellInfoLte cellInfoLte = (CellInfoLte) all.get(0);
        CellSignalStrengthLte cellSignalStrengthLte = cellInfoLte.getCellSignalStrength();
        signal = String.valueOf(cellSignalStrengthLte.getDbm() + " dB");
    } else if (a.equals("android.telephony.CellInfoWcdma")) {
        CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) all.get(0);
        CellSignalStrengthWcdma cellSignalStrengthWcdma = cellInfoWcdma.getCellSignalStrength();
        signal = String.valueOf(cellSignalStrengthWcdma.getDbm() + " dB");

    } else if (a.equals("android.telephony.CellInfoGsm")) {
        CellInfoGsm cellInfoGsm = (CellInfoGsm) all.get(0);
        CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();
        signal = String.valueOf(cellSignalStrengthGsm.getDbm() + " dB");
    }
}

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

发布于 2017-07-28 19:29:09

试试这个:

代码语言:javascript
复制
private static boolean isAirplaneModeOn(Context context) {
    return Settings.System.getInt(context.getContentResolver(),
       Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}

如果启用,则为true。

票数 0
EN

Stack Overflow用户

发布于 2017-07-28 19:57:07

对于网络检查,请尝试如下:

代码语言:javascript
复制
ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

要检查特定的网络类型,如Wifi,请使用以下命令:

代码语言:javascript
复制
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

有关更多细节,请参考https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

适用于飞机模式:

方法1:在运行时检查

代码语言:javascript
复制
public static boolean IsAirplaneModeOn(Context activityContext) {        
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return Settings.System.getInt(context.getContentResolver(), 
                Settings.System.AIRPLANE_MODE_ON, 0) != 0;          
    } else {
        return Settings.Global.getInt(context.getContentResolver(), 
                Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
    }       
}

方法2:广播接收机注册:

代码语言:javascript
复制
IntentFilter airplaneModeIntentFilter = new IntentFilter("android.intent.action.AIRPLANE_MODE");

BroadcastReceiver AirplaneModeReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
            boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
            // isAirplaneModeOn == true, means Airplane mode is turned on.
            // Else AirplaneMode is turned off.
      }
};

activitycontext.registerReceiver(receiver, airplaneModeIntentFilter);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45380995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档