网络监听器Android

时间:2009-11-23 13:26:08

标签: java android networking android-networking

我想查看Android手机网络何时关闭。我可以捕获那个事件吗?

我没有得到正确的API或任何可以解释相同的例子。如果有人做过或任何示例链接真的很有帮助。

4 个答案:

答案 0 :(得分:145)

新的java类:

public class ConnectionChangeReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive( Context context, Intent intent )
  {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(     ConnectivityManager.TYPE_MOBILE );
    if ( activeNetInfo != null )
    {
      Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
    }
    if( mobNetInfo != null )
    {
      Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
    }
  }
}

“manifest”元素下AndroidManifest.xml中的新xml:

<!-- Needed to check when the network connection changes -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

AndroidManifest.xml中“application”元素下的新xml:

<receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver"
          android:label="NetworkConnection">
  <intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
  </intent-filter>
</receiver>

答案 1 :(得分:16)

我一直在使用小型设置来检查带宽,以确定如何缩放比例,例如图像。

根据活动,在AndroidManifest中:

<intent-filter>
...
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>

在执行检查的活动中:

boolean network;
int bandwidth;

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    network = isDataConnected();
    bandwidth = isHighBandwidth();
    registerReceiver(new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            network = isDataConnected();
            bandwidth = isHighBandwidth();
        }
    }, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
    ...
}
...
private boolean isDataConnected() {
    try {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo().isConnectedOrConnecting();
    } catch (Exception e) {
        return false;
    }
}

private int isHighBandwidth() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info.getType() == ConnectivityManager.TYPE_WIFI) {
        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        return wm.getConnectionInfo().getLinkSpeed();
    } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getNetworkType();
    }
    return 0;
}

然后使用示例:

if (network) {
    if (bandwidth > 16) {
        // Code for large items
    } else if (bandwidth <= 16 && bandwidth > 8) {
        // Code for medium items
    } else {
        //Code for small items
    }
} else {
    //Code for disconnected
}

它不是最漂亮的,但它允许足够的灵活性,我可以根据它们的内容和我对它们的要求来改变项目的带宽截止。

答案 2 :(得分:11)

如果您使用Android Annotations是一个选项,请在您的活动中尝试这一点 - 全部,其余部分都会生成:

@Receiver(actions = ConnectivityManager.CONNECTIVITY_ACTION,
        registerAt = Receiver.RegisterAt.OnResumeOnPause)
void onConnectivityChange() {
    //react
}

只有在你已经使用AndroidAnnotations的情况下才使用它 - 只在这段代码中将这个依赖项放在你的项目中就太过分了。

答案 3 :(得分:9)

上述答案仅适用于启用移动数据包数据的情况。否则,ConnectivityManager将为null,您无法再检索NetworkInfo。解决它的方法是使用PhoneStateListener或TelephonyManager。