Android仍然可以在没有连接蜂窝网络的情况下检测蜂窝信

时间:2015-11-26 06:34:12

标签: android telephonymanager

我写了一个小程序来检测细胞信号强度。我将过期的SIM卡插入手机,状态栏显示零信号。但是,以下代码返回信号强度等于 22

public class WelcomeActivity extends AppCompatActivity {
    TelephonyManager telephonyManager;
    CustomPhoneStateListener customPhoneStateListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        customPhoneStateListener = new CustomPhoneStateListener();
        telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(customPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }

    @Override
    protected void onPause() {
        super.onPause();
        telephonyManager.listen(customPhoneStateListener, PhoneStateListener.LISTEN_NONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        telephonyManager.listen(customPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }


    /* Phone State Listener */
    private class CustomPhoneStateListener extends PhoneStateListener {
        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);

            int strength = signalStrength.getGsmSignalStrength();
            Toast.makeText(getApplicationContext(), "Signal Strength: " + String.valueOf(strength), Toast.LENGTH_SHORT).show();
        }
    }
}

22的价值来自哪里?如何检测蜂窝网络是否已连接?这个值真的可靠吗?

从Android documentationgetGsmSignalStrength()返回0到31和99的值。

1 个答案:

答案 0 :(得分:2)

我刚刚实施了n运行代码:

package com.example.sanketprabhu.gsmsignalstrength;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
    {
        TelephonyManager Tel;
        MyPhoneStateListener MyListener;

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            MyListener   = new MyPhoneStateListener();
            Tel       = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
            Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        }

         @Override
        protected void onPause()
        {
            super.onPause();
            Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
        }

          @Override
        protected void onResume()
        {
            super.onResume();
            Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        }

        public  class MyPhoneStateListener extends PhoneStateListener
        {
            /* Get the Signal strength from the provider, each tiome there is an update */
            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength)
            {
                super.onSignalStrengthsChanged(signalStrength);
                Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM Cinr = " 
                        + String.valueOf(signalStrength.getGsmSignalStrength()), Toast.LENGTH_SHORT).show();
            }

        }

    }

它对我有用。

我附上了一些屏幕截图。

哟是对的:0-31& 99

不幸的是,我在办公室,信号强度很低,但工作

image