今天給手上的Nexus6P手機升級系統至Android7.1,體驗了一下新功能:App Shortcuts(圖標快捷方式),以下圖所示:html
如何實現這樣的快捷方式呢?java
官方給出的實現步驟:App Shortcuts | Android Developersandroid
圖標快捷方式(App Shortcuts)分爲兩種:git
Static shortcuts(靜態快捷方式)github
Dynamic shortcuts(動態快捷方式)app
靜態快捷方式是寫在xml文件中,而動態快捷方式是在Java代碼中編寫.gradle
只有Android7.1(25)及以上手機才能使用該功能ui
要求SDK版本爲25及以上this
最低兼容版本爲23(由於動態快捷方式使用Icon類)spa
module 的build.gradle配置以下:
apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.0" defaultConfig { applicationId "net.devwiki.shortcuts" minSdkVersion 23 targetSdkVersion 25 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:25.0.0' testCompile 'junit:junit:4.12' }
1.在項目的res/文件夾下創建 xml目錄
2.在xml目錄建立shortcuts.xml文件,內容以下
<?xml version="1.0" encoding="utf-8"?> <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:shortcutId="compose" android:enabled="true" android:icon="@drawable/ic_share_red_700_48dp" android:shortcutShortLabel="@string/activity_short_label" android:shortcutLongLabel="@string/activity_long_label" android:shortcutDisabledMessage="@string/activity_disable_message"> <intent android:action="android.intent.action.VIEW" android:targetPackage="net.devwiki.shortcuts" android:targetClass="net.devwiki.shortcuts.StaticActivity" /> <!-- 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. --> </shortcuts>
屬性含義和圖片說明以下:
shortcutId : 快捷方式的ID enabled : 是否可用,不可用時點擊Toast提示shortcutDisabledMessage icon : 快捷方式圖標 shortcutShortLabel : 快捷圖標放置到桌面時的標題 shortcutLongLabel : 長按圖標出現快捷方式時的標題 shortcutDisabledMessage : 快捷圖標禁用時的提示語
3.在AndroidManifest.xml文件中設置靜態快捷方式
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.devwiki.shortcuts"> <application android: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-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" /> </activity> <activity android:name=".StaticActivity"> </activity> </application> </manifest>
動態快捷方式即在代碼中添加,更新快捷圖標.
ShortcutManager
提供了添加,移除,更新,禁用,啓動,獲取靜態快捷方式,獲取動態快捷方式,獲取固定在桌面的快捷方式等方法.
可在Java代碼中動態對圖標快捷方式進行操做.
添加快捷方式
private void addShortcut() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { List<ShortcutInfo> infoList = shortcutManager.getDynamicShortcuts(); String shortcutId = null; for (ShortcutInfo shortcutInfo : infoList) { if (ShortcutsId.WEB_DEVWIKI.equals(shortcutInfo.getId())) { shortcutId = shortcutInfo.getId(); } } if (shortcutId == null) { ShortcutInfo shortcut = new ShortcutInfo.Builder(this, ShortcutsId.WEB_DEVWIKI) .setShortLabel(getString(R.string.blog_short_label)) .setLongLabel(getString(R.string.blog_long_label)) .setIcon(Icon.createWithResource(this, R.drawable.ic_blog_logo)) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.devwiki.net"))) .build(); shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut)); Toast.makeText(this, R.string.add_shortcut_success, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, R.string.shortcut_already_exist, Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, R.string.not_support_function, Toast.LENGTH_SHORT).show(); } }
移除快捷方式
private void removeShortcut() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { List<ShortcutInfo> infoList = shortcutManager.getDynamicShortcuts(); String shortcutId = null; for (ShortcutInfo shortcutInfo : infoList) { if (ShortcutsId.WEB_DEVWIKI.equals(shortcutInfo.getId())) { shortcutId = shortcutInfo.getId(); } } if (shortcutId == null) { Toast.makeText(this, R.string.shortcut_not_exist, Toast.LENGTH_SHORT).show(); } else { shortcutManager.removeDynamicShortcuts(Arrays.asList(shortcutId)); Toast.makeText(this, R.string.remove_shortcut_success, Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, R.string.not_support_function, Toast.LENGTH_SHORT).show(); } }
項目代碼在此處:Shortcuts