如何在android中监控网络状态

时间:2012-09-13 08:57:21

标签: android android-networking

如何监控活动中的网络状态?我正在使用谷歌聊天应用程序。

我想只在网络状态发生变化时执行这些行。

if(...){
    GTalkChatDetails.chat(getApplicationContext());
    GTalkChatDetails.adddMsgListener(getApplicationContext());
}

1 个答案:

答案 0 :(得分:0)

创建一个类来寻找信号变化,

private class SignalStrengthListener extends PhoneStateListener 
    {
        @Override
        public void onSignalStrengthsChanged(android.telephony.SignalStrength signalStrength) 
        {
            // get the signal strength (a value between 0 and 31)
            int strengthAmplitude = signalStrength.getGsmSignalStrength();
            // do something with it
            GTalkChatDetails.chat(getApplicationContext());
                    GTalkChatDetails.adddMsgListener(getApplicationContext());
            super.onSignalStrengthsChanged(signalStrength);
        }
    }