.net barcode reader camera Building a background weather service in Java

Encoding Quick Response Code in Java Building a background weather service

Building a background weather service
Painting QR-Code In Java
Using Barcode generator for Android Control to generate, create Denso QR Bar Code image in Android applications.
www.OnBarcode.com
PDF417 Generation In Java
Using Barcode creator for Android Control to generate, create PDF417 image in Android applications.
www.OnBarcode.com
In a basic Android application, you create Activity classes and move from screen to screen using Intent calls, as we ve done in previous chapters. This approach works for the canonical Android screen-to-screen foreground application, but it doesn t work for cases like ours where we want to always listen for changes in the weather, even if
Encode Barcode In Java
Using Barcode generator for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Drawing Barcode In Java
Using Barcode printer for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Intents and Services
Encoding Data Matrix 2d Barcode In Java
Using Barcode generation for Android Control to generate, create Data Matrix image in Android applications.
www.OnBarcode.com
Encoding EAN / UCC - 14 In Java
Using Barcode printer for Android Control to generate, create USS-128 image in Android applications.
www.OnBarcode.com
the user doesn t currently have our app open. For this, we need a Service. In this section, we ll implement the WeatherAlertService we launched in listing 4.4. This Service sends an alert to the user when it learns of severe weather in a specified location. This alert will display over any application, in the form of a Notification, if severe weather is detected. Figure 4.5 shows the notification we ll send. A background task is typically a process that doesn t involve direct user interaction or any type of UI. This process perfectly describes checking for severe weather. After a Service is started, it runs until it s explicitly stopped or the system kills it. The WeatherAlertService background task, which starts when the device boots via the BroadcastReceiver from listing 4.5, is shown in the following listing.
Code 128 Code Set C Generation In Java
Using Barcode encoder for Android Control to generate, create Code 128B image in Android applications.
www.OnBarcode.com
Identcode Maker In Java
Using Barcode creation for Android Control to generate, create Identcode image in Android applications.
www.OnBarcode.com
Listing 4.6
Generating QR In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create QR Code image in ASP.NET applications.
www.OnBarcode.com
Read QR Code ISO/IEC18004 In Visual Basic .NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Figure 4.5 Warning from a background application about severe weather
Code 39 Drawer In Java
Using Barcode maker for Java Control to generate, create Code39 image in Java applications.
www.OnBarcode.com
Barcode Maker In Java
Using Barcode drawer for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
WeatherAlertService class, used to register locations and send alerts
Code128 Maker In C#
Using Barcode creation for VS .NET Control to generate, create Code 128 image in .NET applications.
www.OnBarcode.com
Generating UPC-A Supplement 2 In None
Using Barcode drawer for Office Excel Control to generate, create UPC Code image in Office Excel applications.
www.OnBarcode.com
public class WeatherAlertService extends Service { private static final String LOC = "LOC"; private static final String ZIP = "ZIP"; private static final long ALERT_QUIET_PERIOD = 10000; private static final long ALERT_POLL_INTERVAL = 15000; public static String deviceLocationZIP = "94102"; private Timer timer; private DBHelper dbHelper; private NotificationManager nm; private TimerTask task = new TimerTask() { public void run() { List<Location> locations = Get locations with dbHelper.getAllAlertEnabled(); alerts enabled for (Location loc : locations) { WeatherRecord record = loadRecord(loc.zip); if (record.isSevere()) { if ((loc.lastalert + WeatherAlertService.ALERT_QUIET_PERIOD) < System.currentTimeMillis()) { loc.lastalert = System.currentTimeMillis(); dbHelper.update(loc); sendNotification(loc.zip, record); Fire alert } if severe } } . . . device location alert omitted for brevity } }; private Handler handler = new Handler() { public void handleMessage(Message msg) {
Paint UPC A In None
Using Barcode encoder for Microsoft Word Control to generate, create GS1 - 12 image in Word applications.
www.OnBarcode.com
Encode Barcode In None
Using Barcode creator for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Building a background weather service
Data Matrix ECC200 Generation In Java
Using Barcode printer for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
Printing Data Matrix 2d Barcode In Java
Using Barcode encoder for BIRT Control to generate, create Data Matrix 2d barcode image in Eclipse BIRT applications.
www.OnBarcode.com
notifyFromHandler((String) msg.getData() .get(WeatherAlertService.LOC), (String) msg.getData() .get(WeatherAlertService.ZIP)); }
QR Code JIS X 0510 Encoder In None
Using Barcode generator for Online Control to generate, create QR-Code image in Online applications.
www.OnBarcode.com
Paint Code 39 Extended In Java
Using Barcode generation for BIRT reports Control to generate, create Code 39 Extended image in BIRT applications.
www.OnBarcode.com
}; @Override public void onCreate() { dbHelper = new DBHelper(this); Initialize timer timer = new Timer(); timer.schedule(task, 5000, WeatherAlertService.ALERT_POLL_INTERVAL); nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } . . . onStart with LocationManager and LocationListener \ omitted for brevity @Override public void onDestroy() { Clean up super.onDestroy(); database connection dbHelper.cleanup(); } @Override public IBinder onBind(Intent intent) { return null; } protected WeatherRecord loadRecord(String zip) { final YWeatherFetcher ywh = #10 new YWeatherFetcher(zip, true); return ywh.getWeather(); } private void sendNotification(String zip, WeatherRecord record) { #11 Message message = Message.obtain(); Bundle bundle = new Bundle(); bundle.putString(WeatherAlertService.ZIP, zip); bundle.putString(WeatherAlertService.LOC, record.getCity() + ", " + record.getRegion()); message.setData(bundle); Display actionable handler.sendMessage(message); notification } private void notifyFromHandler(String location, String zip) { Uri uri = Uri.parse("weather://com.msi.manning/loc zip=" + zip); Intent intent = new Intent(Intent.ACTION_VIEW, uri); PendingIntent pendingIntent = PendingIntent.getActivity(this, Intent.FLAG_ACTIVITY_NEW_TASK, intent,PendingIntent.FLAG_ONE_SHOT); final Notification n = new Notification(R.drawable.severe_weather_24, "Severe Weather Alert!", System.currentTimeMillis()); n.setLatestEventInfo(this, "Severe Weather Alert!", location, pendingIntent); nm.notify(Integer.parseInt(zip), n); }
Notify UI from handler
Intents and Services
WeatherAlertService extends Service. We create a service in a way that s similar to
how we ve created activities and broadcast receivers: extend the base class, implement the abstract methods, and override the lifecycle methods as needed. After the initial class declaration, we define several member variables. First come constants that describe our intervals for polling for severe weather and a quiet period. We ve set a low threshold for polling during development severe weather alerts will spam the emulator often because of this setting. In production, we d limit this to check every few hours. Next, our TimerTask variable will let us periodically poll the weather. Each time the task runs, it gets all the user s saved locations through a database call B. We ll examine the specifics of using an Android database in chapter 5. When we have the saved locations, we parse each one and load the weather report. If the report shows severe weather in the forecast, we update the time of the last alert field and call a helper method to initiate sending a Notification C. After we process the user s saved locations, we get the device s alert location from the database using a postal code designation. If the user has requested alerts for his current location, we repeat the process of polling and sending an alert for the device s current location as well. You can see more details on Android location-related facilities in chapter 11. After defining our TimerTask, we create a Handler member variable. This variable will receive a Message object that s fired from a non-UI thread. In this case, after receiving the Message, our Handler calls a helper method that instantiates and displays a Notification D. Next, we override the Service lifecycle methods, starting with onCreate. Here comes the meat of our Service: a Timer E that we configure to repeatedly fire. For as long as the Service continues to run, the timer will allow us to update weather information. After onCreate, we see onDestroy, where we clean up our database connection F. Service classes provide these lifecycle methods so you can control how resources are allocated and deallocated, similar to Activity classes. After the lifecycle-related methods, we implement the required onBind method. This method returns an IBinder, which other components that call into Service methods will use for communication. The WeatherAlertService performs only a background task; it doesn t support binding, and so it returns a null for onBind. We ll add binding and interprocess communication (IPC) in section 4.5. Next, we implement our helper methods. First, loadRecord calls out to the Yahoo! Weather API via YWeatherFetcher. (We ll cover networking tasks, similar to those this class performs, in chapter 6.) Then sendNotification configures a Message with location details to activate the Handler we declared earlier. Last of all, you see the notifyFromHandler method. This method fires off a Notification with Intent objects that will call back into the WeatherReporter Activity if the user clicks on the Notification G. Now that we ve discussed the purpose of services and you ve created a Service class and started one via a BroadcastReceiver, we can start looking at how other developers can interact with your Service.
Copyright © OnBarcode.com . All rights reserved.