Tracking Service Changes

The onServiceStateChanged handler tracks the service details for the device's cell service. Use the ServiceState parameter to find details of the current service state.

The getState method on the Service State object returns the current service state as one of the following ServiceState constants:

> state_in_service Normal phone service is available.

> state_emergency_only Phone service is available but only for emergency calls.

> state_out_of_service No cell phone service is currently available.

> state_power_off The phone radio is turned off (usually when airplane mode is enabled).

A series of getOperator* methods is available to retrieve details on the operator supplying the cell phone service, while getRoaming tells you if the device is currently using a roaming profile.

Listing 12-11 shows how to register for service state changes and displays a Toast showing the operator name of the current phone service.

LISTING 12-11: Monitoring service state changes

Available for download on Wrox.com

PhoneStateListener serviceStateListener = new PhoneStateListener() { public void onServiceStateChanged(ServiceState serviceState) { if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) { String toastText = serviceState.getOperatorAlphaLong(); Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT)

telephonyManager.listen(serviceStateListener,

PhoneStateListener.LISTEN_SERVICE_STATE) ;

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment