I. Introduction of Shortcut
Shortcut is a new feature of Android 7.1 (API level 25), similar to Apple's 3D Touch, but not pressure-sensitive, just a long-press menu. Shortcut is limited by the initiator, that is, the domestic manufacturers of custom systems are mostly not supported, those so-called can pin on the desktop application function of the Quick Start icon is essentially Shortcut.
The realization analysis of Shortcut in Xamarin.Forms
This article discusses the implementation of dynamic Shortcut.
The way of realization is nothing but two ideas, one Native to forms , the other forms to Native . The blogger first considered the Forms to Native, but did not succeed. A Intent is required when setting shortcutinfo, where one of the constructors is
public Intent(Context packageContext, Type type);
It's easy to see, just pass in a Content and typeof the corresponding page, but it throws an exception. The reason is that the incoming Forms Page class is not a native type of Java. Consult the relevant documentation for Xamarin.android to find that this type must inherit the Activity class. So, all forms pages are not allowed, and forms to Native this road can not go.
What about Native to Forms?
Now that you need to rely on activity, you can call the Forms page by creating a new Android activity.
Third, the code implementation
Create a new empty Cross-platform project Shortcutdemo below and share your code with shared project. (Github:https://github.com/zhanggaoxing/xamarin-forms-demo/tree/master/shortcutdemo)
Modify Shared Project
Add two contentpage as a test.
Modify Xamarin.android
Added two activities, ShortcutContainerActivity.cs and FormsActivity.cs.
ShortcutContainerActivity.cs
ShortcutContainerActivity.cs is used as a springboard for displaying Forms pages, so it changes its inherited Activity to global: Xamarin.Forms.Platform.Android.FormsAppCompatActivity . Also change the OnCreate code to the following
protected override void OnCreate(Bundle savedInstanceState){ TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); Intent intent = Intent; // 获取传进来的页面名称 string pageName = intent.GetStringExtra("PageName"); var app = new App(); // 设置显示的页面 switch (pageName) { case "Page1": app.MainPage = new ShortcutDemo.Views.Page1(); break; case "Page2": app.MainPage = new ShortcutDemo.Views.Page2(); break; default: break; } LoadApplication(app);}
It should be noted that the Activity on the top of the tag to change, in addition to Mainlauncher to false, all the others to be the same as in the MainActivity.cs , otherwise it will throw an exception, it may be the theme is not unified cause.
[Activity(Label = "ShortcutDemo", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
FormsActivity.cs
FormsActivity.cs as an activity to start the app normally, just peel it off from the MainActivity.cs. The code is as follows:
[Activity(Label = "ShortcutDemo", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]public class FormsActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity{ protected override void OnCreate(Bundle savedInstanceState) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); }}
MainActivity.cs
MainActivity.cs as an entry for an application, the inheritance of MainActivity.cs can be changed to the Activity class because the initialization of Forms and the loading has been stripped into FormsActivity.cs.
Add a Setshortcut () method to set the Shortcut. First, add a List to hold the Shortcutinfo, in case the last dynamic setting Shortcut as a parameter passed in.
List<ShortcutInfo> shortcutInfoList = new List<ShortcutInfo>();
Next, instantiate a Intent. Wherein SetClass will launch the springboard activity shortcutcontaineractivity,setaction must be set , or the error will not know what is going on; PutExtra used to pass parameters to the next activity, The name we pass in here is used to set the MainPage in the springboard activity.
Intent page1 = new Intent();page1.SetClass(this, typeof(ShortcutContainerActivity));page1.SetAction(Intent.ActionMain);page1.PutExtra("PageName", "Page1");
Shortcutinfo is instantiated below. Setrank to set the ordered ordinal, up to 5 Shortcut, which is 0-4; SetIcon is the set icon; Setshortlabel and Setlonglabel are set long name and segment name; Setintent instantiate the previous step Inte NT incoming, and finally joins it to List.
ShortcutInfo page1Info = new ShortcutInfo.Builder(this, "Page1").SetRank(0).SetIcon(Icon.CreateWithResource(this, Resource.Drawable.Page1)).SetShortLabel("Page1").SetLongLabel("Page1").SetIntent(page1).Build();shortcutInfoList.Add(page1Info);
Finally get Shortcutmanager to set up dynamically Shortcut
ShortcutManager shortcutManager = (ShortcutManager)GetSystemService(Context.ShortcutService);shortcutManager.SetDynamicShortcuts(shortcutInfoList);
So the full MainActivity.cs code is as follows:
[Activity (Label = "Shortcutdemo", Icon = "@drawable/icon", Theme = "@style/maintheme", Mainlauncher = True, Configurationc Hanges = Configchanges.screensize | Configchanges.orientation)]public class mainactivity:activity{protected override void OnCreate (bundle bundle) { Base. OnCreate (bundle); Setshortcut (); StartActivity (typeof (Formsactivity)); } private void Setshortcut () {list<shortcutinfo> shortcutinfolist = new list<shortcutinfo> (); Intent Page1 = new Intent (); Page1. SetClass (This, typeof (Shortcutcontaineractivity)); Page1. Setaction (Intent.actionmain); Page1. PutExtra ("PageName", "Page1"); Shortcutinfo page1info = new Shortcutinfo.builder (This, "Page1"). Setrank (0). SetIcon (Icon.createwithresource (this, Resource.Drawable.Page1)). Setshortlabel ("Page1"). Setlonglabel ("Page1"). Setintent (Page1). Build (); Shortcutinfolist.ADD (Page1info); Intent Page2 = new Intent (); Page2. SetClass (This, typeof (Shortcutcontaineractivity)); Page2. Setaction (Intent.actionmain); Page2. PutExtra ("PageName", "Page2"); Shortcutinfo Page2 = new Shortcutinfo.builder (This, "Page2"). Setrank (1). SetIcon (Icon.createwithresource (this, Resource.Drawable.Page2)). Setshortlabel ("Page2"). Setlonglabel ("Page2"). Setintent (Page2). Build (); Shortcutinfolist.add (Page2); Shortcutmanager Shortcutmanager = (shortcutmanager) getsystemservice (Context.shortcutservice); Shortcutmanager.setdynamicshortcuts (shortcutinfolist); }}
Four
Zhang Happy xamarin.forms Development Note: Android shortcut Shortcut App