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 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.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 the required stack sizes for a software project is a crucial part of the development process. The developer aims to create a stable application, while not wasting resources. This ...
This paper gives an introduction to the epipolar geometry, and provides a complete review of the current techniques for estimating the fundamental matrix and its uncertainty. A well-founded measure is...
For the improvement of reliability, safety and efficiency advanced methods of supervision, fault-detection and fault diagnosis become increasingly important formany technical processes. This holds ...
Monitoring and Managing Your Internet Connectivity 345 Managing Active Connections 346 Managing Your Wi-Fi 347 Controlling Device Vibration 350 Summary 351 Chapter 11: Advanced Android ...
MBA投资课程的经典课本 外文版的,直接购买要好几百RMB
Solving the process of hysteresis without determining the optimal thresholds
dobkin__munro___Determining the Mode.pdfdobkin__munro___Determining the Mode.pdfdobkin__munro___Determining the Mode.pdfdobkin__munro___Determining the Mode.pdfdobkin__munro___Determining the Mode.pdf
Analyzing and determining the correct OSI layer Explaining the basics of network theory and concepts Summarizing safety practices Installing and configuring equipment in the appropriate location ...
•Determining Whether the Device Is Ready •Retrieving Information About the Device •Creating a Persistent Navigation System •Detecting the Device’s Network Status •Detecting When the Network...
Core Files and Directories of the Android Application 73 Creating an AVD for Your Project 75 Creating a Launch Configuration for Your Project 75 Running Your Android Application in the Emulator 76 ...
Identifying the Site Development Process and Players 102 Defining Your Site’s Information Architecture 103 Auditing an Existing Site to Identify SEO Problems 108 Identifying Current Server Statistics...
reducible, polynomial degrees of difficulty are defined, and it is shown that the problem of determining tautologyhood has the same polynomial degree as the problem of determining whether the first of...
the speed, the torque of the vehicle model and the gear status. Additionally the local view of the clutch actuator is shown with the cylinder pressure, the clutch position and the clutch capacity ...
that had taken place in a landmark since designation and determining the use of the landmark. Effecting changes in landmark structures is not only wholly possible, but has been constant and widespread...
gmw 3205 Determining the Resistance to Odor Propagation of Interior Materials.pdf
GMW 3232-2020 Test Method for Determining the.pdf
GMW 3390-2012 Procedure for Determining the Flexibility.pdf
美国红皮书,Methods for determining and processing probabilities,用于研究三维污染物扩散研究,小尺度情况,考虑地形因素,物理碰撞等等
美国紫皮书,Methods for determining and processing probabilities,用于研究三维污染物扩散研究,小尺度情况,考虑地形因素,物理碰撞等等
Human Language Technology (HLT) and Natural Language Processing (NLP) systems have typically focused on the "factual" aspect of content analysis. Other aspects, including pragmatics, opinion, and ...