Introduced
Shortcut is Google's 3D touch but no pressure sensor in API25. The wirelessly is a long press.
Let's look at the effect:
Isn't it great? So please study with this article.
Update
-
New project under the Build.gradle under your project
The following is a simple update to your compilation tool and the specified project version
- Compilesdkversion 25
- Buildtoolsversion "25.0.0"
- Minsdkversion 25
- Targetsdkversion 25
-
Updated Platform-tools to 25
Open SDK Manager
If your Android SDK Platform-tools is less than 25 then tick the bottom right corner and click Update
Static notation
Static writing? The same is true of Broadcastreceiver (broadcast recipient). A register broadcast in a manifest file we call it static, and registering with code is called dynamic
-
Create an XML folder in Res
-
Create a new file under Res/xml named My_shortcut.xml string seemingly must refer to a method such as @string/xxxx
Specific content
<shortcuts xmlns:android="Http://schemas.android.com/apk/res/android"> <shortcut android: Shortcutid = "settings" android:enabled = android:icon =" @mipmap/ic_launcher " android:shortcutshortlabel = "@string/my_short" android:shortcutlonglabel =< Span class= "Hljs-value" > "@string/my_long" android:shortcutdisabledmessage
= "@string/my_disable" ; <intent android:action
= "Android.intent.action.VIEW" android:targetp Ackage = "com.example.administrator.myapplication" android:targetclass =" Com.example.administrator.myapplication.MainActivity "/> <intentandroid:action="Android.intent.action.VIEW"android:targetpackage ="Com.example.administrator.myapplication"android:targetclass=" Com.example.administrator.myapplication.SettingsActivity " /> <categories android:name="android.shortcut.conversation"/> </shortcut></Shortcuts>
Parameter description
Shortcut Property Description:
Android:shortcutid is an ID flag after the dynamic registration will be talked about
Android:enabled is available if not available then this shortcut will not be displayed
Android:shortcutshortlabel Short name: Did you notice the first one? The shortcut is to be out of the way to become a desktop shortcut icon. So this is the name of the icon.
Android:shortcutlonglabel: Quick long name pop up each shortcut name in the list box
Android:shortcutdisabledmessage: When the shortcut is not available, the user will be prompted to click on the text after the dynamic will detail
Intent Property Description:
Hypothesis 1:shortcut (see clearly not shorcuts no s OH) there is only one intent then the result: the user clicks this shortcut user jumps to the activity which the intent formulates
Hypothesis 2: There are two intent undershortcut we named intent1 and Intent2 in order so the user clicks on the shortcut will jump to Intent2 at this point, if the user presses the back key (return key) then it will jump to the intent1 interface
Categories Property Description
Anyway, one of the values is written on it.
-
Registering in the manifest file
Note A small pit: the registration information must be written down in the activity's root tag register for the activity for the startup item<meta-data>
<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/my_shortcut"/> </activity>
The procedure is as follows:
Below the wrong number of "android.app.shortcuts" less hit a s (computer is too card, do not want to re-record) Note!!!!!!!!
-
Effect Show:
-
Small knowledge points
If: You open the shortcut item of the application where there are more than one activity in the fallback stack, guess what? Here is the reader's own attempt. Even if there's nothing against you, you can add a memory.
Dynamic notation-Add
Features as flexible as broadcast recipients
The core code (this example simply clicks the "create" button to perform the following method to generate the shortcut):
//Dynamic Add Public void Onclick2(View view) {Mshortcutmanager = Getsystemservice (Shortcutmanager.class); list<shortcutinfo> infos =NewArraylist<> ();//shortcut can only be up to 5 //Getmaxshortcutcountperactivity can only return 5 for(inti =0; I < mshortcutmanager.getmaxshortcutcountperactivity (); i++) {Intent Intent =NewIntent ( This, Settingsactivity.class); Intent.setaction (Intent.action_view); Intent Intent2 =NewIntent (); Intent2.setaction ("Fmy.fmy"); Intent2.setclassname (Getpackagename (), getpackagename () +". Mainactivity.java "); Intent[] intents =Newintent[2];//Start clicking on shortcut when jumping in this return key to jump into Intent2 other similarintents[0]=intent; intents[1]=intent2;//First parameter context //second parameter IDShortcutinfo info =NewShortcutinfo.builder ( This,"id"+ i). Setshortlabel ("short name."+i+""). Setlonglabel ("long name:"+ i+""). SetIcon (Icon.createwithresource ( This, R.mipmap.ic_launcher))//. Setintent (Intent). Setintents (intents). Build (); Infos.add (info); } mshortcutmanager.setdynamicshortcuts (Infos); }
Effect:
Parsing: I did not click on the "Create" button directly on the desktop press the button to find that nothing happens. Then go to the program press the Create button and return to desktop discovery can be long by the point of the shortcut
Attention and static writing together of the pit (count my experience it):
The pits we've been through all those years->> the above code dynamically creates 5 Quick Click item. But if you write a shortcut item at this time, then congratulations on your red. (abnormal)
caused by:java.lang.IllegalArgumentException:Max number of dynamic shortcuts exceeded
FIX: Get the number of quick item you already have and then either remove the original or reduce the amount you created. or update this is why I know that only 5 reasons can be created. Solution readers read "Dynamic notation-update (overwrite)" and "Dynamic writing-delete" and naturally understand that if you want to solve the problem, copy it directly. Update and delete part of the core code
Dynamic notation-Update (overwrite)
If you want to change the name or intent of certain shortcut item (intent) At some point, please refer to the following code
Public void Onclick3(View view) {Intent Intent2 =NewIntent (); Intent2.setaction ("Fmy.fmy"); Intent2.setclassname (Getpackagename (), getpackagename () +". Mainactivity.java ");//Set ID ID1 will overwrite the original shortcut item for the shortcut with ID id1//If not, nothing will happen .Shortcutinfo info =NewShortcutinfo.builder ( This,"ID1"). Setintent (Intent2). Setlonglabel ("Dynamically updated long name"). Setshortlabel ("Short name for dynamic Update"). build (); Mshortcutmanager = Getsystemservice (Shortcutmanager.class); list<shortcutinfo> dynamicshortcuts = Mshortcutmanager.getdynamicshortcuts (); Mshortcutmanager.updateshortcuts (Arrays.aslist (info)); }
Effect:
Dynamic notation-delete (not available)
Below a short description of the reproduced (I do not want to write, and again the author wrote this description is very good this paragraph describes the original author address)
Let's first introduce a noun-pinning shortcuts, what is this thing? In fact, for shortcut, Android is allowed to directly put on the desktop, so that the user's operation is more convenient, Google said he as pinning shortcuts, concrete what kind, we come to a map on the understanding.
For this pinning shortcuts, Google's documentation says that we developers have no right to delete it, only the user 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 has taken this into account and it allows us to disable this shortcut. Make it gray when the user clicks, a small toast is prompted.
Okay, quote over, thanks to the original author.
We long press on the desktop to drag out the shortcut item to the desktop this item object is Shortcutinfo
The code is the best teacher:
//Delete Public void onclick(View view) {Mshortcutmanager = Getsystemservice (Shortcutmanager.class);//Get all the shortcut item pulled out (if an item is not pulled out then the return length is 0)list<shortcutinfo> infos = Mshortcutmanager.getpinnedshortcuts ();//Traverse all the pulled item and make it gray not clickable for(Shortcutinfo Info:infos) {//At this time the shortcut item is pulled into a gray user then click will pop up toast content for the second parameter "No click Oh" //At this time the desktop long press the original program icon pop-up shortcut list is no longerMshortcutmanager.disableshortcuts (Arrays.aslist (Info.getid ()),"no clicks!"); list<shortcutinfo> dynamicshortcuts = Mshortcutmanager.getdynamicshortcuts (); LOG.E ("TAG","Size"+dynamicshortcuts.size ());//Although not visible but you still have to remove something in the dynamically added list but I call Disableshortcuts . ///after finding its size changed. This method should be called internally. Because the computer is too card download source code, so be sure to write it .Mshortcutmanager.removedynamicshortcuts (Arrays.aslist (Info.getid ())); } list<shortcutinfo> dynamicshortcuts = Mshortcutmanager.getdynamicshortcuts (); LOG.E ("TAG","Size"+dynamicshortcuts.size ()); }
Small knowledge points
- When the user deletes the data, it is dragged out and the shortcut item is deleted.
- The item you created dynamically when the user deletes the data you have on the desktop in the long Press program icon also does not need to be re-written
- The user is dragged out of the uninstall shortcut item is deleted
About this blog post
I happened to see this. 7.1 New features so I've been looking for learning materials. Then I want to write down a few articles and write down the experience. This blog uses the emulator to download the image with the SDK manager because only it has 7.1 mirrors genymotion the latest is only 7.0 It's just. Run Google's own image and its memory consumption I have 4G of memory Stuio and blog and simulator memory only the degree of 80MB card can be imagined. But always wanted to write a high-quality blog post. So the bite of the bullet was stuck for 5 hours. Due to the haste and omission of time, Because the card to not dare not click on the source to see and I did not download. If the above words to you have so little help will be my greatest comfort;
Android 7.1 new features shortcut