Weather on Demand is one of the most common applications of SMS. Few of weather service examples are:
1. Weather alerts from Verizon .
2. Weather Network and TELUS Mobility's On Demand Weather service and
3. Weather.com's Radar on Mobile Phones.
With a simple MIDlet you can get Weather forcasts free on your phone. US government provides free weather forecasts here. Note that the weather forecasts are not as high quality as commercial services. The simple MIDlet here displays weather at SF(modify the URL to point to your city):
001 import java.io.IOException;
002 import java.io.InputStream;
003
004 import javax.microedition.io.Connector;
005 import javax.microedition.io.StreamConnection;
006 import javax.microedition.lcdui.Command;
007 import javax.microedition.lcdui.CommandListener;
008 import javax.microedition.lcdui.Display;
009 import javax.microedition.lcdui.Displayable;
010 import javax.microedition.lcdui.Form;
011 import javax.microedition.midlet.MIDlet;
012
013
014 /**
015 * MIDlet to display weather forecast for SF.
016 *
017 * @author Kumar Mettu
018 * @version 1.0
019 *
020 */
021 public class Weather extends MIDlet implements CommandListener {
022
023 private Display display;
024 //Exit command.
025 private Command exitCommand = new Command( "Exit", Command.SCREEN, 1 );
026 //SF Weather URL.
027 private static final String SF_URL =
028 "http://weather.noaa.gov/pub/data/forecasts/city/ca/san_francisco.txt";
029
030 /**
031 * Weather MIDlet constructor.
032 */
033 public Weather() {
034 display = Display.getDisplay(this);
035 }
036
037 /**
038 * This will be invoked when we start the MIDlet.
039 * Display the downloaded data. If an error occurs while downloading
040 * the data display the error to user.
041 *
042 */
043 public void startApp() {
044 try {
045 getViaStreamConnection();
046 } catch (IOException e) {
047 Form forecast = new Form("Error Downloading");
048 forecast.append(e.getMessage());
049 forecast.addCommand( exitCommand );
050 forecast.setCommandListener( this );
051 display.setCurrent(forecast);
052 }
053 }
054
055 /**
056 * Pause, discontinue ....
057 */
058 public void pauseApp() {
059
060 }
061
062 /**
063 * Destroy. Cleanup everything.
064 */
065 public void destroyApp(boolean unconditional) {
066 }
067
068 /**
069 * Download the weather and display it.
070 *
071 * @throws IOException when IO Error occurs while downloading
072 * data from URL.
073 */
074 private void getViaStreamConnection() throws IOException {
075 StreamConnection sc = null;
076 InputStream in = null;
077 StringBuffer b = new StringBuffer();
078
079 try {
080 sc = (StreamConnection)Connector.open(SF_URL);
081 in = sc.openInputStream();
082 int ch;
083 while ((ch = in.read()) != -1) {
084 b.append((char) ch);
085 }
086
087 } finally {
088 if (in != null) {
089 in.close();
090 }
091 if (sc != null) {
092 sc.close();
093 }
094 }
095
096 Form forecast = new Form("Weather at SF");
097 forecast.append(b.toString());
098 forecast.addCommand( exitCommand );
099 forecast.setCommandListener( this );
100 display.setCurrent(forecast);
101 }
102
103 /**
104 * Indicates that a command event has occurred on Displayable d.
105 * @param c - a Command object identifying the command.
106 * @param d - the Displayable on which this event has occurred.
107 */
108 public void commandAction( Command c, Displayable d ){
109 if (c == exitCommand){
110 exit();
111 }
112 }
113
114 /**
115 * Distroy app when exit button is used.
116 */
117 private void exit(){
118 destroyApp(true);
119 notifyDestroyed();
120 }
121
122 }
|
You can download
Weather.jar and
Weather.jad directly. Tested on my
i88s. Here is the screen snapshot of Motorola i88s simulator:
This can be further extended to support features like:
1. Select the city where you want weather forcast(using kxml)
2. If your phone supports GPS( like
my Motorola i88s), you can display weather forecasts for your current location.
No comments:
Post a Comment