我想知道信号电平。为此,我使用以下方法。但是我不知道如何联系电话,如果它是在飞机模式或停止服务。你能帮帮我吗?
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");
}
}谢谢你的帮助。
发布于 2017-07-28 19:29:09
试试这个:
private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}如果启用,则为true。
发布于 2017-07-28 19:57:07
对于网络检查,请尝试如下:
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();要检查特定的网络类型,如Wifi,请使用以下命令:
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;有关更多细节,请参考https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html。
适用于飞机模式:
方法1:在运行时检查
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:广播接收机注册:
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);https://stackoverflow.com/questions/45380995
复制相似问题