android中的SignalStrength没有显示出来

时间:2013-05-03 09:22:58

标签: android phone-state-listener

我已经编写了一个简单的应用程序来获取android中的信号强度....但是当我尝试在TextView中显示它时,信号强度没有显示...请让我知道是什么问题。 (源代码见下文)

*public class SignalStrengthActivity extends Activity {

    private int intSignalStrength;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signal_strength);
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        //telephonyManager.listen(new MySignalStrengthlistner(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        WifiManager wifiManager;

        if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI){
            wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            Toast.makeText(getApplicationContext(), "Signal Strength Wifi: "+wifiManager.getConnectionInfo().getLinkSpeed(), Toast.LENGTH_LONG).show();
            //networkDetailsVO.setSignalStrength(wifiManager.getConnectionInfo().getLinkSpeed());
        }else if(networkInfo.getType() == ConnectivityManager.TYPE_MOBILE){
            telephonyManager.listen(new MySignalStrengthlistner(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        }

        TextView textView = (TextView) findViewById(R.id.signalStrength);
        //Toast.makeText(getApplicationContext(), "Signal Strength Mobile: "+intSignalStrength, Toast.LENGTH_LONG).show();
        textView.setText(intSignalStrength);

    }



    private class MySignalStrengthlistner extends PhoneStateListener{


            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {

                super.onSignalStrengthsChanged(signalStrength);
                //Toast.makeText(getApplicationContext(), "Signal Strength Mobile: "+signalStrength.getGsmSignalStrength(), Toast.LENGTH_LONG).show();

                intSignalStrength = signalStrength.getGsmSignalStrength();
            }

    }
}*





*<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.pervazive.fetchsignalstrength"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
        <!-- <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> -->
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.fetchsignalstrength.SignalStrengthActivity"
                android:configChanges="orientation|keyboardHidden|screenSize"
                android:label="@string/app_name"
                android:theme="@style/FullscreenTheme" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>*

2 个答案:

答案 0 :(得分:0)

请看一下这堂课,我觉得这会对你有帮助。

http://developer.android.com/reference/android/telephony/SignalStrength.html

答案 1 :(得分:0)

尝试将textView.setText(intSignalStrength)放在onSignalsChanged()类的PhoneStateListener方法中。我遇到了类似的问题,这解决了我。