Reprint Please specify source: http://blog.csdn.net/yyh352091626/article/details/68962736
-
- Shortcut concept
- Specific implementation
- Buildconfig Configuration
- Static configuration
- Dynamic configuration
- Create
- Remove or disable
- Update
- return stack problem
Shortcut concept
Shortcut is a new shortcut component for iOS-like features in Android-25 (Android 7.1), 3D Touch
but it has different manifestations because Android does not support touch pressure sensing on hardware, so the representation is a long press, And the iOS must be pressed long and hard.
First, take a
In the Launcher or apps list, press and hold the app icon to pop up a list of shortcuts, and 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 recreate it.
Specific implementation of Buildconfig configuration
Under the main module, modify the Build.grade so that it compiles using the android-25 API, and of course, without downloading, you need to open the Android SDK Manager to download it.
android { 25 "25.0.0"// 或以上 defaultConfig { 25 }}
Static configuration
Similar to Broadcastreceiver,shortcut registration is also divided into static registration and dynamic registration, first introduce static registration, dynamic registration back to continue ~ ~
res/xml
create an XML underneath the folder, for 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 each shortcut name in the list box
6, Shortcutshortlabel: Shortcut is can be displayed on the desktop alone, the display named Shortcutshortlabel
7, Targetclass: Click the shortcut to enter the activity
8, categories default Write Dead Can
Manifest file Registration
Add a label 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! can go to the desktop to see the effect ~ ~
Dynamic configuration
Dynamic creation increases the flexibility of menu configuration, such as the ability to pull a list of shortcuts from the server and then display them. Here's how to configure it:
Create
Add the following code where you want to register:
/** * Dynamically created * / Public void Register() {Shortcutmanager Mshortcutmanager = Getsystemservice (Shortcutmanager.class); list<shortcutinfo> infos =NewArraylist<> ();//Press the Back button to jump to 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-publish posts"). Setlonglabel ("Create dynamically-publish posts"). SetIcon (Icon.createwithresource ( This, R.mipmap.ic_bar_detail_write)). Setintents (intents). Build (); Infos.add (info); Mshortcutmanager.setdynamicshortcuts (infos); }
Re-run the app and press the long button again to see the following effects:
Remove or disable
Dynamic Delete Deletes a shortcut to a dynamic configuration.
/** * Dynamic Delete * / Public void Delete() {Shortcutmanager Mshortcutmanager = Getsystemservice (Shortcutmanager.class);/********* Remove pop-up list icon **********/ //All dynamic 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 and does not have much to do with the narrative. Must pay attention getPinnedShortcuts
to getDynamicShortcuts
the difference between methods and methods! After disabling the effect, the icon turns gray:
Update
The uniqueness of the shortcut is determined by the identifier mentioned earlier, shortcutId
so the update shortcut is the same as creating a shortcut, overwriting the shortcutId
previously created shortcut if it is the same!
return stack problem
When opened by a shortcut, the existing activity is destroyed and the activity stack is recreated. Because the intent of the shortcut keys set by the manifest way cannot be customized intent flag, its default flag is the FLAG_ACTIVITY_NEW_TASK
and FLAG_ACTIVITY_CLEAR_TASK
.
By means of dynamic registration, it can be found that we can configure the return target activity. Of course, static configuration can also be implemented, modify the shortcut tag:
<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