The latest from Telecom-Funda
Interop 2011: Slide Show | Top |
Android Programming - Part 4 - The Telephony Event Handler | Top |
In part 3 I've looked at the entry method of my app that is called whenever the app is started and how it registers a listener object to network status events. In this part I'll take a look at the listener object itself. The code for the listener object is part of my app and is a private class that is only visible to my app but not to the outside world. Here's a code snippet that shows how it looks like: private class MyPhoneStateListener extends PhoneStateListener { /* Get the Signal strength from the provider, each time there is an update */ @Override public void onSignalStrengthsChanged(SignalStrength signalStrength) { [...] try { /* output signal strength value directly on canvas of the main activity */ outputText = "v7, number of updates: " + String.valueOf(NumberOfSignalStrengthUpdates); NumberOfSignalStrengthUpdates += 1; outputText += "\r\n\r\nNetwork Operator: " + Tel.getNetworkOperator() + " "+ Tel.getNetworkOperatorName() + "\r\n"; outputText += "Network Type: " + String.valueOf(Tel.getNetworkType()) + "\r\n\r\n" ; [...] To keep things simple the app only uses one listener method in the listener object, "onSignalStrengthsChanges". As the name implies this method is called whenever the signal strength of the current cell changes. It is "overridden" as I don't want Android to execute the default actions of this method but I want to do my own things in my instance of the class. When this method is called I also get other network status information and compare it to previously received values. If, for example, the cell-id has changed, I increase a corresponding counter variable accordingly. Reading network status information seems to be a dangerous thing. If network coverage is lost and the method is called for whatever reason, one of the status query methods throws an error, known as an "exception" in Java. If that exception is not handled, the application will be terminated with an error message presented to the user. To prevent that from happening, all network status querries are performed in a "try - catch" construct. If an exception occurs in the "try" part of the code, execution continues in the "catch" part where the exception can be handled. In the case of this app, the code for the "catch" part is rather short as it doesn't do anything with the exception and just hopes for a better day when the device finds the network again. To retrieve the network status information I am interested in, I call the following methods provided by the Android API: TelephonyManager.getNetworkOperator() TelephonyManager.getNetworkOperatorName() TelephonyManager.getNetworkOperatorType() SignalStrength.getGsmSignalStrength() --> works for UMTS as well... GsmCellLocation.getCid() GsmLellLocation.getLac() There's also objects and methods in the API to get information about neighboring cells. However, this doesn't seem to be implemented consistently accross different devices and network technologies. In fact I tried this on several different types and models and only one would give me neighboring cell information and then only for GSM but not for UMTS. Here's the code for it: /* Neighbor Cell Stuff */ List | |
CREATE MORE ALERTS:
Auctions - Find out when new auctions are posted
Horoscopes - Receive your daily horoscope
Music - Get the newest Album Releases, Playlists and more
News - Only the news you want, delivered!
Stocks - Stay connected to the market with price quotes and more
Weather - Get today's weather conditions
You received this email because you subscribed to Yahoo! Alerts. Use this link to unsubscribe from this alert. To change your communications preferences for other Yahoo! business lines, please visit your Marketing Preferences. To learn more about Yahoo!'s use of personal information, including the use of web beacons in HTML-based email, please read our Privacy Policy. Yahoo! is located at 701 First Avenue, Sunnyvale, CA 94089. |
No comments:
Post a Comment