Monitoring time

For the next four recipes, we will use Intents to build the application imagined by Vince Vaughn in the movie The Internship. This application is fairly simple and can be summarized as follows. If you send a text message to someone between 12 a.m. and 6 a.m., well, it is probably a bad idea as you might be quite influenced by alcohol. The solution? You have to solve an equation to prove that you're OK!

On a serious note, here's what we will have to do:

  1. Check the time and redirect the user from the default text message app to ours if the time is between 12 a.m. and 6 a.m
  2. Propose a fairly simple equation for the user to solve
  3. Pick a contact in the contact list held on the phone
  4. Write a text message
  5. Send it to the selected contact

In this recipe, we start with the building of our application by getting the time of the day using a broadcast receiver.

Getting ready

To follow this recipe, create a new project called TheInternship.

How to do it...

We will do the time monitoring using the following steps:

  1. Press the Ctrl + N sequence and add a new Broadcast Receiver project named TimeReceiver to your project.
    How to do it...
  2. Adapt the created class so that it looks like the following:
    [BroadcastReceiver]
    public class TimeReceiver : BroadcastReceiver {
    
      public override void OnReceive (Context context, Intent intent) {
        Console.WriteLine ("Time Received");
        //If we are in the dangerous timeframe
        if (DateTime.Now.Hour >= 0 && DateTime.Now.Hour <= 6) {
          Console.WriteLine ("We are in the dangerous hours");
        }
      }
    }
  3. Register the TimeReceiver class as a TimeTick broadcast receiver in the OnCreate() method of your MainActivity class:
    protected override void OnCreate (Bundle bundle) {
    
      base.OnCreate (bundle);
    
      //Register our broadcast receiver. It will be triggered when the time change, every minute.
      RegisterReceiver (new TimeReceiver (), new IntentFilter (Intent.ActionTimeTick));
    
    }
  4. Run your application. The following statements will be printed in the Application Output tab of Xamarin Studio:
    Console.WriteLine ("Time Received");
    Console.WriteLine ("We are in the dangerous hours");

How it works...

The TimeReceiver class extends the BroadcastReceiver class. The BroadcastReceiver class is a base class that can intercept intent broadcast calls. Android OS contains a huge number of intent broadcast calls, such as onBootCompleted and onTimeChanged, which we use in this recipe.

With the following statement, we check if the current time is between 12 a.m. and 6 a.m. and print a logging line to the console if this is the case.

if (DateTime.Now.Hour >= 0 && DateTime.Now.Hour <= 6) {
  Console.WriteLine ("We are in the dangerous hours");
}

This code takes place in the OnReceive() method and is called every minute. Indeed, the ActionTimeTick broadcast call also happens every minute. Nonetheless, for our BroadcastReceiver class to actually receive broadcast calls, we have to register it. This is what we did in the MainActivity class with the following statements:

RegisterReceiver (new TimeReceiver (), new IntentFilter (Intent.ActionTimeTick));

The RegisterReceiver() method is responsible for registering a new receiver that can be set for broadcast calls or local calls. In our case, we register a new TimeReceiver object, and we add an IntentFilter argument specifying that our receiver is only interested in the ActionTimeTick broadcast call.

Note

Broadcast receiver is a very elegant way to monitor events in the Android System without using a while (true) {//Some Check} instance, which will drain the phone's battery. However, they can only be used when you want to monitor something linked to a broadcast event.

See also

Refer to the Application monitoring recipe to see how we can monitor the running application on the user's phone.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.214.32