O'Reilly logo

Android Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 19. System and Device Control

19.1. Introduction: System and Device Control

Ian Darwin

Discussion

Android provides a good compromise between the needs of the carriers for control and the needs of developers for device access. This chapter looks at some of the informational and control APIs that are publicly available to the Android developer to explore and control the extensive hardware facilities provided by the system, and to deal with the wide range of hardware it runs on, from 2-inch cell phones to 10-inch tablets and netbooks.

19.2. Accessing Phone Network/Connectivity Information

Amir Alagic

Problem

You want to find information about the device’s current network connectivity.

Solution

You can determine whether your phone is connected to the network, its type of connection, and whether your phone is in roaming territory, using the ConnectivityManager and a NetworkInfo object.

Discussion

Often you need to know whether the device you are running on can connect to the Internet at the moment, and, since roaming can be expensive, it is also very useful if you can tell the user whether he is roaming (the user who is truly worried about this will disable data roaming using the Settings application). To do this and more we can use the NetworkInfo class in the android.net package, as in Example 19-1.

Example 19-1. Getting network information
 ConnectivityManager connManager = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = connManager.getActiveNetworkInfo(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required