Determining and Monitoring the Connectivity Status 根据网络连接状况去省电
来源:互联网 发布:hdp直播软件下载 编辑:程序博客网 时间:2024/06/03 17:28
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?http://blog.csdn.net/sergeycao
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 theConnectivityManager
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.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.
- Determining and Monitoring the Connectivity Status 根据网络连接状况去省电
- android开发省电之--Determining and Monitoring the Connectivity Status(根据网络连接状况去省电)
- Determining and Monitoring the Connectivity StatusAndroid 电源管理专题之获取和监测网络连接状态
- [c++] Monitoring the Computer's Power Status and Lid status
- Determining and Monitoring the Docking State and TypeAndroid 电源管理专题之获取和监测Dock状态和类型
- Determining and Monitoring the Docking State and Type 判断并监测设备的停驻状态与类型
- 检查网络连接状况
- 查看网络连接状况
- 获取Windows网络连接状况
- 网络连接状况 ping telnet
- Unity判断网络连接状况
- Ping测试网络连接状况
- Multipeer Connectivity (多点连接)网络框架
- 通过Windows Mobile连接管理器建立网络连接【Establishing Network Connectivity with the Windows Mobile Connection Manager】
- Determining the informational, navigational and transactional intent of web queries
- c#使用winnet检测网络连接状况 .
- Reachability实时检测网络连接状况
- 使用java检测网络连接状况
- Git和Repo管理使用简要介绍
- Android学习笔记
- 【km笔记】CSharp--类的属性和字段
- 崩溃分析
- 关于Android线程的几点说明
- Determining and Monitoring the Connectivity Status 根据网络连接状况去省电
- 一些注意事项
- H.264中的基本概念与基本知识
- Manipulating Broadcast Receivers On Demand 按需操控广播接收
- 为什么是“51
- 第八周项目一之类成员函数实现功能
- Creating a View Class 创建一个视图类
- Custom Drawing 自定义绘制
- Android学习笔记