如何在Android中获取手机服务信号强度?

发布于 2024-08-16 11:44:28 字数 632 浏览 6 评论 0原文

我正在尝试编写一个非常简单的 Android 应用程序来检查当前小区的信号强度。到目前为止,我只找到了一个名为 getNeighboringCellInfo() 的东西,但我不确定它是否包含当前单元格。

如何在 Android 中获取当前的手机信号强度?

getNeighborCellInfo() 是否获取当前单元格?根据我所能得到的结果,这似乎并非如此。这是我当前的代码:

List<NeighboringCellInfo> n = tm.getNeighboringCellInfo();

//Construct the string
String s = "";
int rss = 0;
int cid = 0;
for (NeighboringCellInfo nci : n)
{
    cid = nci.getCid();
    rss = -113 + 2*nci.getRssi();
    s += "Cell ID: " + Integer.toString(cid) + "     Signal Power (dBm): " + 
            Integer.toString(rss) + "\n";
}

mainText.setText(s);

I am trying to write a very simple Android application that checks the signal strength of the current cell. So far, I have only found something called getNeighboringCellInfo(), but I'm not really sure if that includes the current cell.

How do I get the CURRENT cell signal strength in Android?

Does getNeighborCellInfo() get the current cell? It doesn't seem like it based on the results that I have been able to get with it. Here's my current code:

List<NeighboringCellInfo> n = tm.getNeighboringCellInfo();

//Construct the string
String s = "";
int rss = 0;
int cid = 0;
for (NeighboringCellInfo nci : n)
{
    cid = nci.getCid();
    rss = -113 + 2*nci.getRssi();
    s += "Cell ID: " + Integer.toString(cid) + "     Signal Power (dBm): " + 
            Integer.toString(rss) + "\n";
}

mainText.setText(s);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

将军与妓 2024-08-23 11:44:28

创建一个 PhoneStateListener 并处理 onSignalStrengthChanged 回调。当您的应用程序初始化时,它应该给您一个初始通知。这是 1.x 中的。在 2.x 中,存在一个关于此的开放问题

create a PhoneStateListener and handle the onSignalStrengthChanged callback. When your app is initialized, it should give you an initial notification. This is in 1.x. in 2.x, there's an open issue about this.

女中豪杰 2024-08-23 11:44:28

此代码可能会有所帮助:

PhoneStateListener phoneStateListener = new PhoneStateListener() {
public void onCallForwardingIndicatorChanged(boolean cfi) {}
public void onCallStateChanged(int state, String incomingNumber) {}
public void onCellLocationChanged(CellLocation location) {}
public void onDataActivity(int direction) {}
public void onDataConnectionStateChanged(int state) {}
public void onMessageWaitingIndicatorChanged(boolean mwi) {}
public void onServiceStateChanged(ServiceState serviceState) {}
public void onSignalStrengthChanged(int asu) {}
};

创建自己的电话状态侦听器后,使用位掩码将其注册到电话管理器以指示要侦听的事件,如以下代码片段所示:

TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
PhoneStateListener.LISTEN_CALL_STATE |
PhoneStateListener.LISTEN_CELL_LOCATION |
PhoneStateListener.LISTEN_DATA_ACTIVITY |
PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR |
PhoneStateListener.LISTEN_SERVICE_STATE |
PhoneStateListener.LISTEN_SIGNAL_STRENGTH);

此外,需要将这些添加到AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

This code may help:

PhoneStateListener phoneStateListener = new PhoneStateListener() {
public void onCallForwardingIndicatorChanged(boolean cfi) {}
public void onCallStateChanged(int state, String incomingNumber) {}
public void onCellLocationChanged(CellLocation location) {}
public void onDataActivity(int direction) {}
public void onDataConnectionStateChanged(int state) {}
public void onMessageWaitingIndicatorChanged(boolean mwi) {}
public void onServiceStateChanged(ServiceState serviceState) {}
public void onSignalStrengthChanged(int asu) {}
};

Once you’ve created your own Phone State Listener, register it with the Telephony Manager using a bitmask to indicate the events you want to listen for, as shown in the following code snippet:

TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
PhoneStateListener.LISTEN_CALL_STATE |
PhoneStateListener.LISTEN_CELL_LOCATION |
PhoneStateListener.LISTEN_DATA_ACTIVITY |
PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR |
PhoneStateListener.LISTEN_SERVICE_STATE |
PhoneStateListener.LISTEN_SIGNAL_STRENGTH);

Also, need to add these to AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
你的往事 2024-08-23 11:44:28

输入图片这里的描述下面的代码将创建类似图片的效果(Android系统单元格信息)

在您正在运行的活动/片段中,创建一个像这样的子类

TextView txtSignalMobile1 = findViewById...;
    class myPhoneStateListener extends PhoneStateListener {
        public int signalStrengthValue;
        public int signalLevel;

        @RequiresApi(api = Build.VERSION_CODES.Q)
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);
            if (signalStrength.isGsm()) {
                if (signalStrength.getGsmSignalStrength() != 99)
                    signalStrengthValue = signalStrength.getGsmSignalStrength() * 2 - 113;
                else{
                    signalStrengthValue = signalStrength.getCellSignalStrengths().get(0).getDbm();
                    signalLevel =signalStrength.getCellSignalStrengths().get(0).getAsuLevel();
                }


            } else {
                signalStrengthValue = signalStrength.getCdmaDbm();
            }
            txtSignalMobile1.setText(signalStrengthValue + "dbm, " + signalLevel + "asu");
        }
    }

在其他地方调用,可能是onCreate或在按钮之后...(使用线程来不断更新值改变)

Thread splashThread2 = new Thread() {
            @Override
            public void run() {
                try {
                    while (!isInterrupted()) {
                        Thread.sleep(1000);

                        requireActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // update TextView here!
                                int signal2 = NetworkUtils.GetWifiSignal(requireContext());
                                txtSignalWifi.setText(signal2 + "/100");
                            }
                        });
                    }
                } catch (Exception ignore) {
                    // when user exit suddenly
                }
            }
        };
        splashThread2.start();

enter image description hereBelow code will create effect like picture (Android system Cell Info)

Inside your running activity/fragment, create a sub class like this

TextView txtSignalMobile1 = findViewById...;
    class myPhoneStateListener extends PhoneStateListener {
        public int signalStrengthValue;
        public int signalLevel;

        @RequiresApi(api = Build.VERSION_CODES.Q)
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);
            if (signalStrength.isGsm()) {
                if (signalStrength.getGsmSignalStrength() != 99)
                    signalStrengthValue = signalStrength.getGsmSignalStrength() * 2 - 113;
                else{
                    signalStrengthValue = signalStrength.getCellSignalStrengths().get(0).getDbm();
                    signalLevel =signalStrength.getCellSignalStrengths().get(0).getAsuLevel();
                }


            } else {
                signalStrengthValue = signalStrength.getCdmaDbm();
            }
            txtSignalMobile1.setText(signalStrengthValue + "dbm, " + signalLevel + "asu");
        }
    }

Call somewhere else maybe onCreate or after a button... (used thread to update continnously value changed)

Thread splashThread2 = new Thread() {
            @Override
            public void run() {
                try {
                    while (!isInterrupted()) {
                        Thread.sleep(1000);

                        requireActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // update TextView here!
                                int signal2 = NetworkUtils.GetWifiSignal(requireContext());
                                txtSignalWifi.setText(signal2 + "/100");
                            }
                        });
                    }
                } catch (Exception ignore) {
                    // when user exit suddenly
                }
            }
        };
        splashThread2.start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文