61

我想检查Android中的手机网络何时关闭。我可以捕捉到那个事件吗?

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

4

4 回答 4

147

新的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();
    }
  }
}

AndroidManifest.xml 中“manifest”元素下的新 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>
于 2009-11-23T19:09:10.927 回答
18

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

在Activity下,在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
}

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

于 2013-01-13T15:08:59.263 回答
11

如果您可以选择使用Android 注释,请在您的活动中尝试此操作 - 仅此而已,其余部分将生成:

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

仅当您已经使用 AndroidAnnotations 时才使用它 - 仅将此依赖项放在您的项目中用于这段代码将是矫枉过正。

于 2014-06-12T12:44:03.230 回答
9

上述答案仅在启用移动数据包数据时才有效。否则,ConnectivityManager 将为空,您无法再检索 NetworkInfo。解决方法是改用 PhoneStateListener 或 TelephonyManager。

于 2012-09-27T16:30:48.007 回答