获取信号强度的Andr​​oid [英] Get Signal Strength in Android

查看:185
本文介绍了获取信号强度的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得设备的信号强度在点我打的API调用。我搜索的所有相关的主题,我不成功呢。

I want to get the Signal Strength of the Device at the point i hit the API call. I have searched on all the related threads and i am not successful yet .

所以,我想获得的信号强度像

So i would like to get the signal strength like

SignalStrength ss = null  ; // some initialization

int n = ss.getGsmSignalStrength();

但在使用这个,很明显,因为我已经初始化 SignalStrength 为空我会得到空指针异常。但我不知道如何初始化这一点。

but while using this , it is obvious that i will get null pointer exception since i have initialised SignalStrength as null . But i don't know how to initialise this.

另外,我不希望使用 PhoneStateListener ,因为它触发只有在信号变化。

Also that i don't want to use PhoneStateListener because it is triggered only if the signal changes .

我使用的是下面的code得到的信号强度

I am getting the Signal Strength using the below code

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下将无法正常工作。我想code工作在API级别7+。

but i don't want to use CellSignalStrength because it is only added in API Level 17 and will not work under 17 . I want the code to work on API Level 7+ .

或者有没有其他的方法,这样我就可以得到信号强度撞击API调用点?

Or is there any other method, so that i could get the signal strength at the point of hitting the API call ?

推荐答案

定义变量:

TelephonyManager TelephonManager;
myPhoneStateListener pslistener;    
int SignalStrength = 0;

那么这个类添加到您的code:

Then add this class to your code:

class myPhoneStateListener extends PhoneStateListener {

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);
            SignalStrength = signalStrength.getGsmSignalStrength();
            SignalStrength = (2 * SignalStrength) - 113; // -> dBm          
        }

    }

和您的的onCreate 使用方法:

     try {
            pslistener = new myPhoneStateListener();
            TelephonManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            TelephonManager.listen(pslistener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
         }
    catch (Exception ex) {

          ex.printStackTrace();

         }

这篇关于获取信号强度的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆