android网络监听器,网络监听器Android

cf423552e4e915eda3e7000fe230142f.png

慕斯王

我一直在使用一个小设置检查带宽,以确定如何缩放比例,如图像。根据活动,在AndroidManifest中:...

    在执行检查的活动中:boolean network;int bandwidth;@Overridepublic 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}它不是最漂亮的,但它允许足够的灵活性,我可以改变物品的带宽截止,这取决于它们是什么以及我对它们的要求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值