静态添加Shortcuts功能
先上效果图:
1.在AndroidManifest.xml文件的主activity下面配置shortcuts,如下图:
主要代码:
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
2.在res文件下创建xml文件夹,创建showrtcuts.xml
然后编写相关的代码(我这里写了三个快捷):
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="compose1"
android:enabled="true"
android:icon="@drawable/ic_menu_joke"
android:shortcutShortLabel="@string/joke"
android:shortcutLongLabel="@string/joke"
android:shortcutDisabledMessage="@string/joke">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.chenxinwen.baiduapi"
android:targetClass="com.chenxinwen.baiduapi.ui.joke.JokeActivity" />
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list is what the user sees when they
launch this shortcut. -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<!-- Specify more shortcuts here. -->
<shortcut
android:shortcutId="compose2"
android:enabled="true"
android:icon="@drawable/ic_menu_gallery"
android:shortcutShortLabel="@string/gallery"
android:shortcutLongLabel="@string/gallery"
android:shortcutDisabledMessage="@string/gallery">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.chenxinwen.baiduapi"
android:targetClass="com.chenxinwen.baiduapi.ui.gallery.GalleryActivity" />
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list is what the user sees when they
launch this shortcut. -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<shortcut
android:shortcutId="compose3"
android:enabled="true"
android:icon="@drawable/ic_menu_slideshow"
android:shortcutShortLabel="@string/province_select"
android:shortcutLongLabel="@string/province_select"
android:shortcutDisabledMessage="@string/province_select">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.chenxinwen.baiduapi"
android:targetClass="com.chenxinwen.baiduapi.ui.province_select.ProvinceActivity" />
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list is what the user sees when they
launch this shortcut. -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
3.在build.gradle中修改compileSdkVersion和buildToolsVersion,至少要是25以上才可以.