网络监听器Android

问题描述 投票:60回答:4

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

我没有得到适当的API或任何可以解释相同的例子。如果有人做过或任何示例链接将真的有用。

java android networking android-networking
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"/>

Android应用程序元素下AndroidManifest.xml中的新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>

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
}

它不是最漂亮的,但它允许足够的灵活性,我可以改变项目的带宽截止,这取决于它们是什么和我对它们的要求。


11
投票

如果使用Android Annotations是一个选项,你可以在你的活动中尝试这一点 - 这就是全部,其余的是生成的:

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

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


9
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.