แชร์ผ่าน


Nougat Features

How to get started using Xamarin.Android to develop apps for Android Nougat.

This article provides an outline of the features introduced in Android Nougat, explains how to prepare Xamarin.Android for Android Nougat development, and provides links to sample applications that illustrate how to use Android Nougat features in Xamarin.Android apps.

Overview

Android Nougat is Google's followup to Android 6.0 Marshmallow. Xamarin.Android provides support for Android 7.x Bindings in Xamarin Android 7.0 and later. Android Nougat adds many new APIs for the Nougat features described below; these APIs are available to Xamarin.Android apps when you use Xamarin.Android 7.0.

Hero images of Android tablets and phones running Android Nougat

For more information about Android 7.x APIs, see Android API Reference. For a list of known Xamarin.Android 7.0 issues, please see the release notes.

Android Nougat provides many new features of interest to Xamarin.Android developers. These features include:

  • Multi-window support – This enhancement makes it possible for users to open two apps on the screen at once.

  • Notification Enhancements – The redesigned notifications system in Android Nougat includes a Direct Reply feature that allow users to quickly respond to text messages directly from the notification UI. Also, if your app creates notifications for received messages, the new bundled notifications feature can bundle notifications together as a single group when more than one message is received.

  • Data Saver – This feature is a new system service that helps reduce cellular data use by apps; it gives users control over how apps use cellular data.

In addition, Android Nougat brings many other enhancements of interest to app developers such as a new network security configuration feature, Doze on the Go, key attestation, new Quick Settings APIs, multi-locale support, ICU4J APIs, WebView enhancements, access to Java 8 language features, scoped directory access, a custom pointer API, platform VR support, virtual files, and background processing optimizations.

This article explains how to get started building apps with Android Nougat to try out the new features and plan migration or feature work to target the new Android Nougat platform.

Requirements

The following is required to use the new Android Nougat features in Xamarin-based apps:

  • Visual Studio or Visual Studio for Mac – If you are using Visual Studio, version 4.2.0.628 or later of Visual Studio Tools for Xamarin is required. If you are using Visual Studio for Mac, version 6.1.0 or later of Visual Studio for Mac is required.

  • Xamarin.Android – Xamarin.Android 7.0 or later must be installed and configured with either Visual Studio or Visual Studio for Mac.

  • Android SDK - Android SDK 7.0 (API 24) or later must be installed via the Android SDK Manager.

  • Java Developer Kit – Xamarin Android 7.0 development requires JDK 8 or later if you are developing for API level 24 or greater (JDK 8 also supports API levels earlier than 24). The 64-bit version of JDK 8 is required if you are using custom controls or the Forms Previewer.

Important

Xamarin.Android does not support JDK 9.

Note that apps must be rebuilt with Xamarin C6SR4 or later to work reliably with Android Nougat. Because Android Nougat can link only to NDK-provided native libraries, existing apps using libraries such as Mono.Data.Sqlite.dll may crash when running on Android Nougat if they are not properly rebuilt.

Getting Started

To get started using Android Nougat with Xamarin.Android, you must download and install the latest tools and SDK packages before you can create an Android Nougat project:

  1. Install the latest Xamarin.Android updates from the Xamarin.

  2. Install the Android 7.0 (API 24) packages and tools or later.

  3. Create a new Xamarin.Android project that targets Android Nougat.

  4. Configure an emulator or device for Android Nougat.

Each of these steps is explained in the following sections:

Install Xamarin Updates

To add Xamarin support for Android Nougat, change the updates channel in Visual Studio or Visual Studio for Mac to the Stable channel and apply the latest updates. If you also need features that are currently available only in the Alpha or Beta channel, you can switch to the Alpha or Beta channel (the Alpha and Beta channels also provide support for Android 7.x). For information about how to change the updates (releases) channel, see Changing the Updates Channel.

Install the Android SDK

To create a project with Xamarin Android 7.0, you must first use the Android SDK Manager to install SDK Platform Android N (API 24) or later. You must also install the latest Android SDK Tools:

  1. Start the Android SDK Manager (in Visual Studio for Mac, use Tools > Open Android SDK Manager…; in Visual Studio, use Tools > Android > Android SDK Manager).

  2. Install Android 7.0 (API 24) or later:

    Selecting Android 7.0 packages in the Android SDK Manager

  3. Install the latest Android SDK tools:

    Selecting the latest Android SDK tools in the Android SDK Manager

    You must install Android SDK Tools revision 25.2.2 or later, Android SDK Platform tools 24.0.3 or later, and Android SDK Build tools 24.0.2 or later.

  4. Verify that the Java Development Kit Location is configured for JDK 1.8:

    Configuring the JDK 8 path under Tools options

    To view this setting in Visual Studio, click Tools > Options > Xamarin > Android Settings. In Visual Studio for Mac, click Preferences > Projects > SDK Locations > Android.

Start a Xamarin.Android Project

Create a new Xamarin.Android project. If you are new to Android development with Xamarin, see Hello, Android to learn about creating Xamarin.Android projects.

When you create an Android project, you must configure the version settings to target Android 7.0 or later. For example, to target your project for Android 7.0, you must configure the target Android API level of your project to Android 7.0 (API 24 - Nougat). It is recommended that you set your target framework level to API 24 or later. For more about configuring Android API level levels, see Understanding Android API Levels.

Note

Currently you must set the Minimum Android version to Android 7.0 (API 24 - Nougat) to deploy your app to Android Nougat devices or emulators.

Configure an Emulator or Device

If you are using an emulator, start the Android AVD Manager and create a new device using the following settings:

  • Device: Nexus 5X, Nexus 6, Nexus 6P, Nexus Player, Nexus 9, or Pixel C.
  • Target: Android 7.0 - API Level 24
  • ABI: x86 or x86_64

For example, this virtual device is configured to emulate a Nexus 6:

Configuring an AVD using Nexus 6 device, Android 7.0 target, and Intel Atom x86 CPU/ABI

If you are using a physical device such as a Nexus 5X, 6, or 9, you can either update your device through automatic over the air (OTA) updates or download a system image and flash your device directly. For more information about manually updating your device to Android Nougat, see OTA Images for Nexus Devices.

Note that Nexus 5 devices are not supported by Android Nougat.

New Features

Android Nougat introduces a variety of new features and capabilities, such as Multi-window Support, Notifications enhancements, and Data Saver. The following sections highlight these features and provide links to help you get started using them in your app.

Multi-Window Mode

Multi-window mode makes it possible for users to open two apps at once with full multitasking support. These apps can run side-by-side (landscape) or one-above-the-other (portrait) in split-screen mode. Users can drag a divider between the apps to resize them, and they can cut and paste content the between apps. When two apps are presented in multi-window mode, the selected activity continues to run while the unselected activity is paused but still visible. Multi-window mode does not modify the Android activity lifecycle.

Example apps running in multi-window mode in both portrait and landscape

You can configure how the activities of your Xamarin.Android app support multi-window mode. For example, you can configure attributes that set the minimum size and the default height and width of your app in multi-window mode. You can use the new Activity.IsInMultiWindowMode property to determine if your activity is in multi-window mode. For example:

if (!IsInMultiWindowMode) {
    multiDisabledMessage.Visibility = ViewStates.Visible;
} else {
    multiDisabledMessage.Visibility = ViewStates.Gone;
}

For more information about multi-window mode, see the Multi-Window Support.

Enhanced Notifications

Android Nougat introduces a redesigned notification system. It features a new Direct Reply feature that makes it possible for users to quickly reply to notifications for incoming text messages directly in the notification UI. Starting with Android 7.0, notification messages can be bundled together as a single group when more than one message is received. Also, developers can customize notification views, leverage system decorations in notifications, and take advantage of new notification templates when generating notifications.

Direct Reply

When a user receives a notification for incoming message, Android Nougat makes it possible to reply to the message within the notification (rather than open up the messaging app to send a reply). This inline reply feature makes it possible for users to quickly respond to an SMS or text message directly within the notification interface:

Screenshot of a notification with an inline Direct Reply field

To support this feature in your app, you must add inline reply actions to your app via a RemoteInput object so that users can reply via text directly from the notification UI. For example, the following code builds a RemoteInput for receiving text input, builds a pending intent for the reply action, and creates a remote input enabled action:

// Build a RemoteInput for receiving text input:
var remoteInput = new Android.Support.V4.App.RemoteInput.Builder (EXTRA_REMOTE_REPLY)
    .SetLabel (GetString (Resource.String.reply))
    .Build ();

// Build a Pending Intent for the reply action to trigger:
PendingIntent replyIntent = PendingIntent.GetBroadcast (ApplicationContext,
                                conversation.ConversationId,
                                GetMessageReplyIntent (conversation.ConversationId),
                                PendingIntentFlags.UpdateCurrent);

// Build an Android 7.0 compatible Remote Input enabled action:
NotificationCompat.Action actionReplyByRemoteInput = new NotificationCompat.Action.Builder (
    Resource.Drawable.notification_icon,
    GetString (Resource.String.reply),
    replyIntent).AddRemoteInput (remoteInput).Build ();

This action is added to the notification:

// Create the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder (ApplicationContext)
   .SetSmallIcon (Resource.Drawable.notification_icon)
   ...
   .AddAction (actionReplyByRemoteInput);

For more information about adding inline reply actions to your app for Android 7.0 or later, see the Android Replying to Notifications topic.

Bundled Notifications

Android Nougat can group notification messages together (for example, by message topic) and display the group rather than each separate message. This bundled notifications feature makes it possible for users to dismiss or archive a group of notifications in one action. The user can slide down to expand the bundle of notifications to view each notification in detail:

Screenshot example of bundled notifications

To support bundled notifications, your app can use the Builder.SetGroup method to bundle similar notifications. For more information about bundled notification groups in Android N, see the Android Bundling Notifications topic.

Custom Views

Android Nougat makes it possible for you to create custom notification views with system notification headers, actions, and expandable layouts. For more information about custom notification views in Android Nougat, see the Android Notification Enhancements topic.

Data Saver

Beginning with Android Nougat, users can enable a new Data Saver setting that blocks background data usage. This setting also signals your app to use less data in the foreground wherever possible. The ConnectivityManager has been extended in Android Nougat so that your app can check whether the user has enabled Data Saver so that your app can make an effort to limit its data usage when Data Saver is enabled.

For more information about the new Data Saver feature in Android Nougat, see the Android Optimizing Network Data Usage topic.

App Shortcuts

Android 7.1 introduced an App Shortcuts feature that makes it possible for users to quickly start common or recommended tasks with your app. To activate the menu of shortcuts, the user long-presses the app icon for a second or more – the menu appears with a quick vibration. Releasing the press causes the menu to remain:

Example screen of an app shortcut menu for a messaging app

This feature is available only API level 25 or higher. For more information about the new App Shortcuts feature in Android 7.1, see the Android App Shortcuts topic.

Summary

This article introduced Android Nougat and explained how to install and configure the latest tools and packages for Xamarin.Android development on Android Nougat. It also provided an overview of the key features available in Android Nougat, with links to example source code to help you get started in creating apps for Android Nougat.