Determining and Monitoring the Connectivity Status

  Some of the most common uses for repeating alarms and background services is to schedule regular updates of application data from Internet resources, cache data, or execute long running downloads. But if you aren't connected to the Internet, or the connection is too slow to complete your download, why both waking the device to schedule the update at all?

  You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place.

Determine if You Have an Internet Connection

  There's no need to schedule an update based on an Internet resource if you aren't connected to the Internet. The following snippet shows how to use the ConnectivityManager to query the active network and determine if it has Internet connectivity.

 ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();

Determine the Type of your Internet Connection

  It's also possible to determine the type of Internet connection currently available.

  Device connectivity can be provided by mobile data, WiMAX, Wi-Fi, and ethernet connections. By querying the type of the active network, as shown below, you can alter your refresh rate based on the bandwidth available.

boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

  Mobile data costs tend to be significantly higher than Wi-Fi, so in most cases, your app's update rate should be lower when on mobile connections. Similarly, downloads of significant size should be suspended until you have a Wi-Fi connection.

  Having disabled your updates, it's important that you listen for changes in connectivity in order to resume them once an Internet connection has been established.

Monitor for Changes in Connectivity

  The ConnectivityManager broadcasts the CONNECTIVITY_ACTION("android.net.conn.CONNECTIVITY_CHANGE") action whenever the connectivity details have changed. You can register a broadcast receiver in your manifest to listen for these changes and resume (or suspend) your background updates accordingly.

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

  Changes to a device's connectivity can be very frequent—this broadcast is triggered every time you move between mobile data and Wi-Fi. As a result, it's good practice to monitor this broadcast only when you've previously suspended updates or downloads in order to resume them. It's generally sufficient to simply check for Internet connectivity before beginning an update and, should there be none, suspend further updates until connectivity is restored.

  This technique requires toggling broadcast receivers you've declared in the manifest, which is described in the next lesson.

Android 性能优化(14)网络优化( 10)Determining and Monitoring the Connectivity Status的更多相关文章

  1. 《Android开发艺术探索》读书笔记 (13) 第13章 综合技术、第14章 JNI和NDK编程、第15章 Android性能优化

    第13章 综合技术 13.1 使用CrashHandler来获取应用的Crash信息 (1)应用发生Crash在所难免,但是如何采集crash信息以供后续开发处理这类问题呢?利用Thread类的set ...

  2. Android性能优化典范(二)

    Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...

  3. android app性能优化大汇总(google官方Android性能优化典范 - 第2季)

    Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...

  4. Android性能优化典范 - 第2季

    Google发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的缩放,缓 ...

  5. Android 性能优化探究

    使用ViewStub动态载入布局.避免一些不常常的视图长期握住引用: ViewStub的一些特点: 1. ViewStub仅仅能Inflate一次,之后ViewStub对象被置空:某个被ViewStu ...

  6. android 性能优化

    本章介绍android高级开发中,对于性能方面的处理.主要包括电量,视图,内存三个性能方面的知识点. 1.视图性能 (1)Overdraw简介 Overdraw就是过度绘制,是指在一帧的时间内(16. ...

  7. Android性能优化典范第一季

    2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...

  8. [转]Android性能优化典范

    2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...

  9. [Android Pro] Android性能优化典范第一季

    reference to : http://www.cnblogs.com/hanyonglu/p/4244035.html#undefined 2015年伊始,Google发布了关于Android性 ...

随机推荐

  1. bat初学

    今天在重置某工具软件试用期时(汗,盗版可耻=_=!,鄙视自己一下),实在懒得每次过了试用期再点开注册表,手工删除了,就想着写个脚本执行一下,这里就想起来了bat文件. 首先介绍一下for循环,从百度知 ...

  2. OpenCV在Linux(Fedora)下搭建开发环境简述

    盼望了好久的Fedora21终于发行了.先来晒一张图: 默认桌面还是那么简洁: 好了,废话少说.来看看在Fedora下搭建opencv开发环境,因为我已经搭建好了(过程比较艰辛) 先注明参考文章,感谢 ...

  3. vue生成包报错error from UglifyJs

    mangle: { keep_fnames: true},

  4. nginx: [emerg] unknown directive "聽" in D:\software03\GitSpace\cms\nginx-1.14.0/conf/nginx.conf:36

    nginx: [emerg] unknown directive "聽" in D:\software03\GitSpace\cms\nginx-1.14.0/conf/nginx ...

  5. linux 的硬链接与软连接

    linux 里有硬链接和软连接两种概念.要明白这些概念首先要明白文件在linux 上其实有3个组成部分. data 真正的数据存储区域 inode 一个用来唯一表示data的数据结构 filename ...

  6. 2014-8-21的一次性能诊断--应用server瓶颈

    今天现场实施反馈系统总体慢.已经接到用户许多的投诉,要求现场发回weblogic日志和Oracle 数据库报告.简要说下系统的架构:典型的B/S三层架构,开发语言是java,中间件用的是weblogi ...

  7. Objective-C 2.0 基础要点归纳

    本文的阅读基本条件: 具备C/C++基础知识,了解面向对象特征 阅读过<Objective-C 2.0 程序设计(第二版)>.<Objective-C 程序设计 第6版>或相关 ...

  8. 网站访问分析对SEO的好处

    统计剖析,应该说是每个SEO都必需要擅长的技艺.至于网站统计的剖析,根据自己的一些经验,与大家分享一下相关技巧.(发表于 2012-3-24 23:12) 申请一个统计帐号很容易,现在有很多的统计服务 ...

  9. debug 和release 的区别

    http://blog.csdn.net/h_wlyfw/article/details/26688677

  10. 创建类模式大PK(总结)

    创建类模式包含工厂方法模式.建造者模式.抽象工厂模式.单例模式和原型模式,它们都可以提供对象的创建和管理职责.当中的单例模式和原型模式很easy理解,单例模式是要保持在内存中仅仅有一个对象,原型模式是 ...