This article has authorized the public number: Yang (hongyangandroid) in the public platform original debut.
Just a few days ago, Google held its autumn press conference, announcing its newest handset brand pixel without any suspense, while my Nexus device was downgraded from a pro-son to a dry son. But fortunately, Google did not treat this worked a pro-android7.1.1, this is not, today pushed the update of the new, with it is a big wave of features (and our developers are busy ...), one of the functions of the call Shortcuts is really awesome, With this feature we will no longer have to page by page to enter the pages we want, in the desktop long press the icon can quickly enter (alas, the function of domestic app is estimated to wait a long time ...), and, this shortcut is our developer to customize! So, on the night of the API release, I learned about this new feature from the documentation, and then I had this article.
Shortcuts First Experience
Well, before we start the code, let's look at the new features, many of the Google apps in the new system are integrated with this feature, so let's pick an app to try it out.
This function is still very practical, with it, we can directly open the text message app to someone to send text messages ~ ~ Below we began to learn how this new shortcuts in the development of how to use!
Using the static shortcuts
What is a static shortcuts? My understanding is to use XML to write dead configuration, think BroadcastReceiver can be static registration can also use Java Code Dynamic registration, here is the same.
What about static registration? First, we need to res/xml create a new XML file in the directory, where we name it shortcuts.xml .
<shortcuts xmlns:android="Http://schemas.android.com/apk/res/android"> <shortcut android: Shortcutid = "settings" android:enabled = android:icon =" @drawable/icon " android:shortcutshortlabel =" @string/settings_short_name " android:shortcutlonglabel = "@string/settings_long_name" android:shortcutdisabledmessage = "@string/settings _disable_msg "; <intentandroid:action="Android.intent.action.VIEW"android:targetpackage ="org.loader.shotcutsstatic"android:targetclass=" Org.loader.shotcutsstatic.SettingsActivity " /> <categories android:name="android.shortcut.conversation"/> </shortcut></Shortcuts>
First a shortcuts label, then a shortcut , here we can probably guess that there can be registered more than one shortcut , the shortcut label has a lot of properties, we come to understand each.
- Shortcutid, needless to say, this is definitely a unique ID.
- Enabled, indicating whether this shortcut is available
- Shortcutshortlabel, here is the short name of the configuration, there will be a long name below, if the long name is not displayed, the short name is displayed
- Shortcutlonglabel, here is the long name of the configuration, launcher will prefer long name display
- Shortcutdisabledmessage, this configuration is a hint to the user when we select an unavailable shortcut
shortcutThere are two more familiar tabs under the label.
- Intent, here is what we click Shortcut when to do, Targetpackage is to specify a target application package name, Targetclass is we want to jump target class, here to pay attention to must be
android:action configured, otherwise it will crash
- Categories, this thing is currently in the official position only to provide
android.shortcut.conversation
OK, the above lines of code, we complete a static shortcuts , how to use it? It is used where activity is configured in manifest, and this activity is required.
the activity that can be configured shortcuts must have action Yes android.intent.action.MAIN and category Yes android.intent.category.LAUNCHER !
Why should have this request, oneself to realize ...
Finally, let's take a look at this configuration:
<applicationandroid:allowbackup="true"android:icon="@ Mipmap/ic_launcher "android:label=" @string/app_name "android:supportsrtl= "true" android:theme="@style/apptheme"> <activity android:name=". Mainactivity "> <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> <meta-dataandroid:name="android.app.shortcuts"android:resource= "@xml/shortcuts"/> </activity> <activity android:name=". Settingsactivity " /> </Application>
To see the effect of the final implementation:
OK, here, static configuration shortcuts We have finished learning, is not very simple? What is this static configuration used for? I think, the scenario here is usually a fixed function, such as your app's settings interface, if it is some dynamic data, the static configuration is not suitable, We need to introduce the dynamic configuration in the next step.
Using the dynamic shortcuts
After reading the static shortcuts, we do not believe that Google only give us developers to open up the limitations of such a large use, there will certainly be more flexible API, yes, this is what we are going to say Dynamic Shortcuts , I call it dynamic configuration.
Speaking of dynamic configuration, it must be implemented in Java code, how to implement it?
First step, we need to use the code to getShortcutManager
getSystemService(ShortcutManager.class)
ShortcutManagerafter we get it, we can call the setDynamicShortcuts(List<ShortcutInfo>) method to set shortcut, how does this list get? Let's take a look at the full point code,
Private void setupshortcuts() {Mshortcutmanager = Getsystemservice (Shortcutmanager.class); list<shortcutinfo> infos =NewArraylist<> (); for(inti =0; I < mshortcutmanager.getmaxshortcutcountperactivity (); i++) {Intent Intent =NewIntent ( This, Messageactivity.class); Intent.setaction (Intent.action_view); Intent.putextra ("MSG","Me and"+ Madapter.getitem (i) +"The Dialogue"); Shortcutinfo info =NewShortcutinfo.builder ( This,"id"+ i). Setshortlabel (Madapter.getitem (i)). Setlonglabel ("Contact:"+ Madapter.getitem (i)). SetIcon (Icon.createwithresource ( This, R.drawable.icon)). Setintent (Intent). Build (); Infos.add (info);//Manager.adddynamicshortcuts (Arrays.aslist (info));} mshortcutmanager.setdynamicshortcuts (infos);}
The background of this code is that we simulated a contact list function, in launcher we long press the icon will appear a certain number of contact shortcuts, click a shortcut will jump directly to the contact related pages. OK, after the introduction of the background, we look at the code, first we getSystemService(ShortcutManager.class) get ShortcutManager , the next for loop, note this for loop number of times, because we want to add Shortcut can not be unlimited, so here we use getMaxShortcutCountPerActivity to get to the maximum number. And then in the For Loop, We first construct a intent, note that here and the Static Shortcut same, you have to provide one Action . Then we use it ShortcutInfo.Builder to construct one ShortcutInfo and put it in the list, and finally we call mShortcutManager.setDynamicShortcuts(infos) to set the shortcuts.
Well, the code is actually very simple, let's take a look at the effect.
Isn't it 666?
Dynamic Update shortcuts
The above code we say is dynamic, but only the implementation of Java code, the real dynamic we are going to explain the next, before we explain the dynamic, we first introduce a noun- Pinning Shortcuts what is this thing? Shortcut Android is to allow us to put directly on the desktop, so that the user's operation is more convenient, Google calls him as Pinning Shortcuts , specifically what kind of, we come to a picture to understand.
For this Pinning Shortcuts , Google's documents say that we developers do not have the right to delete the only users who can delete it. What if I delete this feature? This thing is still on the desktop, is not the app to collapse? Of course, Google takes this into account, which allows us to go to disable this shortcut . Specifically, let's look at the code, where we'll just press the item to simulate the deletion.
privatevoidremoveItem(int index) { List<ShortcutInfo> infos = mShortcutManager.getPinnedShortcuts(); for (ShortcutInfo info : infos) { if (info.getId().equals("id" + index)) { "暂无该联系人"); } } mShortcutManager.removeDynamicShortcuts(Arrays.asList("id" + index));}
First we call mShortcutManager.getPinnedShortcuts() to get all of it, Pinning Shortcuts then go through it, find the one we deleted, then disableShortcuts(List<Ids>) drop the item through the API, and disable finally we'll use it removeDynamicShortcuts(List<Ids>) to remove it from shortcuts the. Look at the effect.
Through the effect, we can see that our disableShortcuts one Pinning Shortcut has become gray, and in the click of the time will be reminded 暂无该联系人 that this reminder is disableShortcuts the second parameter.
Now, delete and disable what we've already learned, what about the update? If I change the name of a contact, is shortcut it appropriate to modify it? Yes, we still need to implement this code.
Private void Upditem(intIndex) {Intent Intent =NewIntent ( This, Messageactivity.class); Intent.setaction (Intent.action_view); Intent.putextra ("MSG","Me and"+ Madapter.getitem (Index) +"The Dialogue"); Shortcutinfo info =NewShortcutinfo.builder ( This,"id"+ index). Setshortlabel (Madapter.getitem (Index)). Setlonglabel ("Contact:"+ Madapter.getitem (index)). SetIcon (Icon.createwithresource ( This, R.drawable.icon)). Setintent (Intent). Build (); Mshortcutmanager.updateshortcuts (Arrays.aslist (info));}
Build intent we will not say, then we use ShortcutInfo.Builder to build a new ShortcutInfo , and finally we have used updateShortcuts(List<ShortcutInfo>) to implement shortcut the update, very simple, to see the effect.
OK, now, the function of Android7.1 Shortcuts We are almost finished, the example code in the article can be downloaded to Https://github.com/qibin0506/Android7_Shortcuts_Demo, Official documents You can also see more, here is the address: https://developer.android.com/preview/shortcuts.html
It's time to get to know Android7: Shortcuts (shortcut)