{"id":28224,"date":"2016-12-20T08:03:53","date_gmt":"2016-12-20T16:03:53","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=28224"},"modified":"2016-12-20T08:03:53","modified_gmt":"2016-12-20T16:03:53","slug":"simple-and-intuitive-app-shortcuts-in-android-7-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/simple-and-intuitive-app-shortcuts-in-android-7-1\/","title":{"rendered":"Simple and Intuitive App Shortcuts in Android 7.1"},"content":{"rendered":"<p>\t\t\t\tAndroid 7.0 Nougat just released with some great new features such as <a href=\"https:\/\/blog.xamarin.com\/enhanced-notifications-in-android-n-with-direct-reply\/\">Direct Reply<\/a> and <a href=\"https:\/\/blog.xamarin.com\/understanding-androids-doze-functionality\/\">Doze enhancements<\/a>, and Android 7.1 is already on the way and in Developer Preview. You don&#8217;t have to wait for the new Android 7.1 (API 25) SDK to be fully released to start integrating and shipping new features today. The top of the features to come to Android in 7.1 is <a href=\"https:\/\/developer.android.com\/preview\/shortcuts.html\">App Shortcuts<\/a> giving your users actions that can quickly be jumped to right from the home screen or app drawer. <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/3edf51ed-45c7-4ed5-8de8-deea5fd14b25.png\" alt=\"3edf51ed-45c7-4ed5-8de8-deea5fd14b25\" width=\"250\" class=\"alignright size-full wp-image-28300\" \/><\/p>\n<h2>Static Shortcuts<\/h2>\n<p>App Shortcuts come in two flavors for developers to implement both static and dynamic. Static is available today, the easiest to implement, and doesn&#8217;t require any new SDK or API to be targeted. Simply add a few Intent Filters and descriptions of the shortcuts and you have instant app shortcuts ready to go. Let&#8217;s take a look at how we could update the <a href=\"https:\/\/blog.xamarin.com\/browse-through-the-evolve-2016-mobile-app-source-code\/\">Evolve conference app<\/a> for Android with new app shortcuts to take any attendee directly to the sessions, events, and mini-hacks.<\/p>\n<h3>Update the Android Manifest<\/h3>\n<p>In the application element of your AndroidManifest.xml change, or add a android:targetSdkVersion attribute, to have a value of 25. This will ensure that Android knows we are using APIs in Android 7.1.<\/p>\n<h3>Update Main Launcher Activity<\/h3>\n<p>To communicate to Android that our application has shortcuts we must specify some additional meta-data for our &#8220;Main Launcher&#8221; of our application. This can be located in the <b>[Activity]<\/b> attribute where <b>MainLauncher=true<\/b>. Under this attribute we can add another attribute called <b>MetaData<\/b> to specify the resource file where our shortcuts will live:<\/p>\n<pre class=\"theme:vs2012 lang:csharp decode:true\">\n[MetaData (\"android.app.shortcuts\", Resource =\"@xml\/shortcuts\")]\n<\/pre>\n<h3>Create a Shotcuts XML<\/h3>\n<p>Under the <b>Resources<\/b> folder in the Android project create a new folder called <b>xml<\/b> and a new XML file insides of it called <b>shortcuts.xml<\/b> This is where we will specify all of the shortcuts that our app can support such as the following:<\/p>\n<pre class=\"theme:vs2012 lang:xml decode:true\">\n\n  \n    \n    <!-- If your shortcut is associated with multiple intents, include them\n         here. The last intent in the list is what the user sees when they\n         launch this shortcut. -->\n    \n  \n  <!-- Specify more shortcuts here. -->\n\n<\/pre>\n<p>Notice here that I specify the <strong>targetClass<\/strong> as my MainActivity of the application that I want to launch. You can specify any activity as long as the <b>Export<\/b> property is set to true. Additionally, it is best practice here to specify the Activities name manually in its attribute:<\/p>\n<pre class=\"theme:vs2012 lang:csharp decode:true\">\n    [Activity(Label = \"Evolve16\", \n        Name=\"com.xamarin.xamarinevolve.MainActivity\",\n        Exported = true,\n        Icon = \"@drawable\/newicon\", \n        LaunchMode = LaunchMode.SingleInstance, \n        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]\n\n<\/pre>\n<h3>Responding to App Shortcuts<\/h3>\n<p>Now that our application has the app shortcuts in place we need to respond to them. Since we passed in <b>data<\/b> as part of each Intent we can simply perform a switch case and perform navigation from our MainActivity&#8217;s OnCreate method:<\/p>\n<pre class=\"theme:vs2012 lang:csharp decode:true\">\nif(string.IsNullOrWhiteSpace(Intent?.Data?.LastPathSegment))\n{\n  switch(Intent.Data.LastPathSegment)\n  {\n    case \"sessions\":\n      MessagingService.Current.SendMessage(\"DeepLinkPage\", new DeepLinkPage\n        { Page = AppPage.Sessions });\n       break;\n    case \"events\":\n      MessagingService.Current.SendMessage(\"DeepLinkPage\", new DeepLinkPage\n        { Page = AppPage.Events});\n      break;\n    case \"minihacks\":\n      MessagingService.Current.SendMessage(\"DeepLinkPage\", new DeepLinkPage\n        { Page = AppPage.MiniHacks});\n      break;\n  }\n}\n<\/pre>\n<p>Based on this message that is received by the Xamarin.Forms <b>App<\/b> class we can easily navigate to any of the pages we need that were launched from app shortcuts.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/ezgif-3874462432.gif\" alt=\"ezgif-3874462432\" width=\"240\" height=\"427\" class=\"aligncenter size-full wp-image-28304\" \/><\/p>\n<h2>Learn More<\/h2>\n<p>You can grab the Evolve app source code from the <b>app-shortcuts<\/b> branch from <a href=\"https:\/\/github.com\/xamarinhq\/app-evolve\/tree\/app-shortcuts\">GitHub<\/a> that has been updated with app shortcuts. That same application also features <a href=\"https:\/\/blog.xamarin.com\/pressure-sensitive-gestures-with-3d-touch-and-ios-9\/\">iOS 3D Touch Quick Actions<\/a> which have a similar style for iOS apps. You can find more about Android 7.1&#8217;s app shortcut features by reading the <a href=\"https:\/\/developer.android.com\/preview\/shortcuts.html\">full documentation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android 7.0 Nougat just released with some great new features such as Direct Reply and Doze enhancements, and Android 7.1 is already on the way and in Developer Preview. You don&#8217;t have to wait for the new Android 7.1 (API 25) SDK to be fully released to start integrating and shipping new features today. The [&hellip;]<\/p>\n","protected":false},"author":544,"featured_media":28300,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5],"class_list":["post-28224","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-android"],"acf":[],"blog_post_summary":"<p>Android 7.0 Nougat just released with some great new features such as Direct Reply and Doze enhancements, and Android 7.1 is already on the way and in Developer Preview. You don&#8217;t have to wait for the new Android 7.1 (API 25) SDK to be fully released to start integrating and shipping new features today. The [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/28224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=28224"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/28224\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=28224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=28224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=28224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}