我希望在点击API调用时得到设备的信号强度。我搜索了所有相关的主题,但我还没有成功。
所以我想获得像
这样的信号强度SignalStrength ss = null ; // some initialization
int n = ss.getGsmSignalStrength();
但是在使用它时,很明显我会得到空指针异常,因为我已将SignalStrength
初始化为null。但我不知道如何初步化。
此外,我不想使用PhoneStateListener
,因为只有在信号发生变化时才会触发它。
我使用以下代码获得信号强度
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
但我不想使用CellSignalStrength
,因为它仅在API级别17中添加,并且在17下无法使用。我希望代码能够在API Level 7+上运行。
或者是否还有其他方法,以便我可以在点击API调用时获得信号强度?
答案 0 :(得分:23)
定义变量:
TelephonyManager mTelephonyManager;
MyPhoneStateListener mPhoneStatelistener;
int mSignalStrength = 0;
然后将此类添加到您的代码中:
class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
mSignalStrength = signalStrength.getGsmSignalStrength();
mSignalStrength = (2 * mSignalStrength) - 113; // -> dBm
}
}
并在onCreate
方法中使用:
mPhoneStatelistener = new MyPhoneStateListener();
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(mPhoneStatelistener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
答案 1 :(得分:6)
全球定义:
TelephonyManager telephonyManager;
myPhoneStateListener psListener;
TextView txtSignalStr;
onCreate
方法:
@Override
protected void onCreate(final Bundle savedInstanceState) {
txtSignalStr = (TextView)findViewById(R.id.signalStrength);
psListener = new myPhoneStateListener();
telephonyManager = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(psListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
创建myPhoneStateListener
班级:
public class myPhoneStateListener extends PhoneStateListener {
public int signalStrengthValue;
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
if (signalStrength.isGsm()) {
if (signalStrength.getGsmSignalStrength() != 99)
signalStrengthValue = signalStrength.getGsmSignalStrength() * 2 - 113;
else
signalStrengthValue = signalStrength.getGsmSignalStrength();
} else {
signalStrengthValue = signalStrength.getCdmaDbm();
}
txtSignalStr.setText("Signal Strength : " + signalStrengthValue);
}
}
答案 2 :(得分:4)
select id, name
from "user"
left join item on item.user_id = "user".id
left join foob on foob.user_id = "user".id
where item.key = 'my_key' and foob.value_t = 'vtz'
group by "user".id
答案 3 :(得分:3)
我们不应该初始化signalstrength,而不是使用phonelistener并覆盖onSignalStrengthsChanged(SignalStrength signalStrength)方法。
例如,请查看以下代码段
class SamplePhoneStateListener extends PhoneStateListener {
int signalStrength = 0;
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
signalStrength = signalStrength.getGsmSignalStrength();
//You can check the signal strength value here..
}
}
使用TelephonyManager对象,您可以听取上面的类
TelephonyManagerObject.listen(myListener,
PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)
答案 4 :(得分:2)
这对我有用:
需要哪些服务:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
如何收集指标:
public void startGatherMetrics() {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
networkInfoStr = connectivityManager.getActiveNetworkInfo().toString();
// gather Network Capabilities
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Network network = connectivityManager.getActiveNetwork();
networkInfoStr += "; " + connectivityManager.getNetworkCapabilities(network).toString();
}
}
Log.d("A_NETWORK_INFO", networkInfoStr);
new Thread(new Runnable() {
@Override
public void run() {
quitLooper = false;
Looper.prepare();
telephonyManager.listen(new PhoneStateListener() {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
Log.d("A_NETWORK_METRICS",
"Signal Strength (0-4 / dBm):" + getLevel(signalStrength) + " / "
+ getDbm(signalStrength));
if (quitLooper)
Looper.myLooper().quit();
}
}, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
Looper.loop();
}
}).start();
}
public void stop() {
quitLooper = true;
}
可以找到“ getLevel(signalStrength) ”和“ getDbm(signalStrength) ” {{ 3}}。
答案 5 :(得分:1)
你没有实例化SignalStrength(可能你不能)。来自应用程序代码。
您必须使用PhoneStateListener(子类),并实现onSignalStrengthsChanged:
将为您创建一个SignalStrength并传递给您的覆盖。
答案 6 :(得分:1)
科林版本:
class myPhoneStateListener : PhoneStateListener() {
var signalStrengthValue = 0
override fun onSignalStrengthsChanged(signalStrength: SignalStrength) {
super.onSignalStrengthsChanged(signalStrength)
signalStrengthValue = if (signalStrength.isGsm) {
if (signalStrength.gsmSignalStrength != 99) signalStrength.gsmSignalStrength * 2 - 113 else signalStrength.gsmSignalStrength
} else {
signalStrength.cdmaDbm
}
txtSignalStr.setText("Signal Strength : $signalStrengthValue")
}
}
答案 7 :(得分:0)
应该在循环程序线程中实例化PhoneStateListener对象。因此,如果您使用工作线程(非GUI线程),则可以使用以下内容;
private HandlerThread handlerThreadCellularSignal = null;
public void startListen() {
handlerThreadCellularSignal = new HandlerThread("CELLULAR_INFO_THREAD");
handlerThreadCellularSignal.start();
Looper looper = handlerThreadCellularSignal.getLooper();
Handler handler = new Handler(looper);
handler.post(new Runnable() {
@Override
public void run() {
phoneStatelistener = new PhoneStateListenerEx();
TelephonyManager telephonyManager = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStatelistener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
});
}
private void stopListen() {
handlerThreadCellularSignal.quit();
}
public class PhoneStateListenerEx extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
int signalStrengthdBm = (2 * signalStrength.getGsmSignalStrength()) - 113; // -> dBm
Log.d("Cellular Signal Strength | " + String.valueOf(signalStrengthdBm));
}
}
答案 8 :(得分:0)
public class myPhoneStateListener extends PhoneStateListener {
public int signalStrengthValue;
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
if (signalStrength.isGsm()) {
if (signalStrength.getGsmSignalStrength() != 99)
signalStrengthValue = signalStrength.getGsmSignalStrength() * 2 - 113;
else
signalStrengthValue = signalStrength.getGsmSignalStrength();
} else {
signalStrengthValue = signalStrength.getCdmaDbm();
}
txtSignalStr.setText("Signal Strength : " + signalStrengthValue);
}
}