Reprint Please specify source: http://blog.csdn.net/yyh352091626/article/details/68962736
-
- Shortcut concept
- Detailed implementation
- Buildconfig Configuration
- Static configuration
- Dynamic configuration
- Create
- Remove or disable
- Update
- return stack problem
Shortcut concept
Shortcut is a shortcut component for the new iOS-like feature of Android-25 (Android 7.1) 3D Touch . But there are different forms of expression. Because Android does not support touch pressure sensing on the hardware, it is represented by a long press, while the iOS must be pressed long and hard.
First of all. Have a
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvexlomzuymdkxnji2/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "width=" 280 "height=" >
In the Launcher or application list. Long press the app icon to bring up a list of shortcuts. Also, you can drag a single shortcut out as a desktop icon, and the dragged icon disappears as you clear the app data or uninstall the app, and you need to create it again.
Detailed implementation of Buildconfig configuration
Under the main module, change the Build.grade to use the android-25 API to compile, of course, not downloaded, you need to open the Android SDK Manager to download.
android { 25 "25.0.0"// 或以上 defaultConfig { 25 }}
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvexlomzuymdkxnji2/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "Width=" >
Static configuration
Similar broadcastreceiver,shortcut are also classified as static and dynamic. First introduce static register, dynamic Register after continue ~ ~
-
res/xmlcreate an XML underneath the directory. Give me a chestnut: shortcut.xml
<shortcuts xmlns:android="Http://schemas.android.com/apk/res/android"> <shortcut android: Enabled = android:icon = "@mipmap/ic_bar_detail_write" android: Shortcutdisabledmessage = "@string/shortcut_publish" android:shortcutid =" publish " android:shortcutlonglabel = "@string/shortcut_ Publish " android:shortcutshortlabel =" @ String/shortcut_publish "; <intentandroid:action="Android.intent.action.VIEW"Android:targetclass ="com.yanshi.writing.ui.bar.PublishPostActivity"android:targetpackage=" Com.yanshi.writing " /> <categories android:name="android.shortcut.conversation" /></shortcut><shortcut android: Enabled = android:icon = "@mipmap/logo" android:shortcutdisabledmessage =< Span class= "Hljs-value" > "@string/shortcut_write" android:shortcutid =< Span class= "Hljs-value" > "write" android:shortcutlonglabel = "@string/shortcut_write" android:shortcutshortlabel =< Span class= "Hljs-value" > "@string/shortcut_write" ; <intentandroid:action="Android.intent.action.VIEW"Android:targetclass ="com.yanshi.writing.ui.write.WriteActivity"android:targetpackage=" Com.yanshi.writing " /> <categories android:name="android.shortcut.conversation" /></shortcut></Shortcuts>
1, Enabled: Indicates whether the current shortcut can be used
2. Icon: Shortcut icons
3. Shortcutdisabledmessage: The name displayed when the shortcut is not available
4. Shortcutid: Shortcut identification
5, Shortcutlonglabel: Long press the bullet out every shortcut name in the list box
6, Shortcutshortlabel: Fast is able to display on the desktop alone, the display named Shortcutshortlabel
7, Targetclass: Click the shortcut to enter the activity
8, categories default Write Dead Can
-
Checklist file
Add tag configuration to the default startup page of Androidmainfest.xml meta-data
<activityandroid:name=". UI. Mainactivity "android:configchanges=" Orientation|screensize|keyboardhidden "android: Screenorientation="Portrait"android:theme="@style/apptheme.nonetranslucent" > <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/shortcut" /> </activity>
- Complete! Able to see the effect on the desktop ~ ~
Dynamic configuration
Dynamic creation adds the flexibility of menu configuration, such as the ability to pull a list of shortcuts from the server and then display them. Detailed configuration methods such as the following:
Create
Add the following code where you need to register:
/** * Dynamically created * / Public void Register() {Shortcutmanager Mshortcutmanager = Getsystemservice (Shortcutmanager.class); list<shortcutinfo> infos =NewArraylist<> ();//Press RETURN button to jump activityIntent intent1 =NewIntent ( This, Mainactivity.class); Intent1.setaction (Intent.action_view);//target activityIntent Intent2 =NewIntent ( This, Publishpostactivity.class); Intent2.setaction ("Com.yuyh.xxx.BACK"); Intent[] intents =Newintent[2]; intents[0] = Intent1; intents[1] = Intent2; Shortcutinfo info =NewShortcutinfo.builder ( This,"Publish-2"). Setshortlabel ("Create dynamically – Post posts"). Setlonglabel ("Create dynamically – Post posts"). SetIcon (Icon.createwithresource ( This, R.mipmap.ic_bar_detail_write)). Setintents (intents). Build (); Infos.add (info); Mshortcutmanager.setdynamicshortcuts (infos); }
Execute the app again, press and hold again, and the effect is as follows:
Remove or disable
Dynamic deletion allows you to delete a shortcut to a dynamic configuration.
/** * Dynamic Delete * / Public void Delete() {Shortcutmanager Mshortcutmanager = Getsystemservice (Shortcutmanager.class);/********* Remove pop-up list icon **********/ //All dynamically create iconslist<shortcutinfo> infos1 = Mshortcutmanager.getdynamicshortcuts (); List<string> IDS1 =NewArraylist<> (); for(Shortcutinfo info:infos1) {Ids1.add (Info.getid ()); }//Disable all ShortcutsMshortcutmanager.disableshortcuts (IDS1,"Disabled"); Mshortcutmanager.removedynamicshortcuts (IDS1);/********* Remove the dragged Desktop shortcut icon **********/ //icons placed on the desktoplist<shortcutinfo> Infos2 = Mshortcutmanager.getpinnedshortcuts (); List<string> IDS2 =NewArraylist<> (); for(Shortcutinfo Info:infos2) {Ids2.add (Info.getid ()); } mshortcutmanager.disableshortcuts (Ids2,"Disabled"); Mshortcutmanager.removealldynamicshortcuts ();}
The code is relatively simple. It's not much of a narrative. Be aware getPinnedShortcuts getDynamicShortcuts of the difference between methods and methods!
After disabling the effect you see, the icon turns gray:
Update
The uniqueness of the shortcut. This is determined by the identifier mentioned earlier shortcutId , so the update shortcut is the same as creating a shortcut. shortcutIdassuming the same, the previously created shortcuts will be overwritten!
return stack problem
When opened by a shortcut. The existing activity will be destroyed, and then another activity stack is created.
Because the intent of the shortcut keys set by the manifest method cannot define intent's flag by itself, its default flag is the FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK .
It can be found through the dynamic register method. We are able to configure the return target activity. Of course, static configuration can also be implemented. Change shortcut Tags:
<shortcutandroid:enabled="true"android:icon="@mipmap/ic_bar_ Detail_write "android:shortcutdisabledmessage=" @string/shortcut_publish "android: Shortcutid="Publish"android:shortcutlonglabel="@string/shortcut_publish" Android:shortcutshortlabel="@string/shortcut_publish"> <!--return target activity -- <intentandroid:action= "Com.yuyh.xxx.BACK"android:targetclass=" Com.yanshi.writing.ui.MainActivity "android:targetpackage=" com.yanshi.writing " /> <!--target activity -- <intentandroid:action="Android.intent.action.VIEW"android: Targetclass="com.yanshi.writing.ui.bar.PublishPostActivity"android:targetpackage= "com.yanshi.writing" /> <categories android:name="android.shortcut.conversation" /> </shortcut>
Thanks for reading.
Android 7.1.1 implementation 3D Touch