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(); ...

Get Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.