Exploring KitKat

KitKat is, at the time of writing this, the latest Android version on the market but should be very soon replaced by Lollipop. Kitkat runs 37.8 % of the Android phone as of November 2014, and therefore, you should be careful while using what it proposes as you are targeting a subset of Android users equipped with last generation phones.

Getting ready

To follow this recipe, create a new project called KitKat and create a new AVD running Android 4.4 to deploy your code. In order to create an AVD running Android 4.4, you'll need to download and install the corresponding SDK using the Android SDK Manager.

How to do it...

  1. In your MainActivity class, add the following code in the OnCreate() method:
    if(Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) {
      Console.WriteLine("I'am KitKat powered");
    }
  2. In your MainActivity class, add the following code in the OnCreate() method:
    Uri uri = Android.Net.Uri.Parse("http://www.packtpub.com");
    Intent myIntentToBeStarted = new Intent(Intent.ActionView, uri);
    AlarmManager alarmManager = (AlarmManager)GetSystemService(AlarmService);
    PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, myIntentToBeStarted, PendingIntentFlags.UpdateCurrent);
    alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + 10000,  pendingIntent);
  3. Drag and drop button to your UI and then create a Ressources/Layout.axml file that contains the following:
    <resources>
      <style name="KitKatTheme" parent="android:Theme.Holo.Light">
        <item name="android:windowBackground">@color/xamgray</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:fitsSystemWindows">true</item>
        <item name="android:actionBarStyle">@style/ActionBar.Solid.KitKat</item>
      </style>
    
      <style name="ActionBar.Solid.KitKat" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
        <item name="android:background">@color/xampurple</item>
      </style>
    </resources>
  4. Run your application. You will see a line printed on the console confirming that the device is powered by KitKat, and ten seconds later, a webpage will open redirecting to the https://www.packtpub.com website. You can also notice some modification to the theme of the application as compared to what we saw earlier. The theme is now translucent.

How it works...

With three simple tags to place in your ressources/layout.axml file, Android KitKat allows us to use the translucent theme that comes with KitKat in your own application:

  • windowTranslucentStatus: If this is set to true, the status bar at the top will be translucent.
  • windowTranslucentNavigation: This is the same as windowTranslucentStatus but for the navigation bar.
  • fitsSystemWindows: If your applications are translucent and so is the OS, then there is a possibility of overlapping. In order to avoid overlapping between translucent applications or applications and the OS, you have to set this argument to true.

In this recipe, we also use an alarm service to browse to the packtpub.com website every half an hour. In KitKat, the management of the alarm service has been improved in order to group several applications that have to be woken up in the same time interval. For example, you can run a bunch of health-check applications every half an hour, and therefore, enhance the battery life, compared to always keeping them active or activating them one by one.

In order to specify the window of time in which your applications have to be awoken at least once, you can use the SetWindow() method of the AlarmManager instance as we did in this recipe:

alarmManager.SetWindow (AlarmType.Rtc, AlarmManager.IntervalHalfHour, AlarmManager.IntervalHour, myIntentToBeStarted);

See also

Go to http://developer.android.com/about/versions/kitkat.html for a complete summary of KitKat features. You can also refer to Chapter 9, Playing with Advanced Graphics, for the animations functionalities and Chapter 11, Using Hardware Interactions, to learn about the sensor interactions of KitKat.

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

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