Android7.1图标快捷方式(AppShortcuts)实现Demo
今天給手上的Nexus6P手機(jī)升級系統(tǒng)至Android7.1,體驗(yàn)了一下新功能:App Shortcuts(圖標(biāo)快捷方式),如下圖所示:
如何實(shí)現(xiàn)這樣的快捷方式呢?
官方給出的實(shí)現(xiàn)步驟:App Shortcuts | Android Developers
分類
圖標(biāo)快捷方式(App Shortcuts)分為兩種:
- Static shortcuts(靜態(tài)快捷方式) 
- Dynamic shortcuts(動態(tài)快捷方式) 
靜態(tài)快捷方式是寫在xml文件中,而動態(tài)快捷方式是在Java代碼中編寫.
實(shí)現(xiàn)
環(huán)境要求
只有Android7.1(25)及以上手機(jī)才能使用該功能
要求SDK版本為25及以上
最低兼容版本為23(因?yàn)閯討B(tài)快捷方式使用Icon類)
module 的build.gradle配置如下:
apply plugin: 'com.android.application'android {compileSdkVersion 25buildToolsVersion "25.0.0"defaultConfig {applicationId "net.devwiki.shortcuts"minSdkVersion 23targetSdkVersion 25versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles 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' }靜態(tài)快捷方式
1.在項(xiàng)目的res/文件夾下建立 xml目錄
2.在xml目錄創(chuàng)建shortcuts.xml文件,內(nèi)容如下
<?xml version="1.0" encoding="utf-8"?> <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><shortcutandroid: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"><intentandroid: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 themhere. The last intent in the list is what the user sees when theylaunch this shortcut. --><categories android:name="android.shortcut.conversation" /></shortcut><!-- Specify more shortcuts here. --> </shortcuts>屬性含義和圖片說明如下:
shortcutId : 快捷方式的ID enabled : 是否可用,不可用時點(diǎn)擊Toast提示shortcutDisabledMessage icon : 快捷方式圖標(biāo) shortcutShortLabel : 快捷圖標(biāo)放置到桌面時的標(biāo)題 shortcutLongLabel : 長按圖標(biāo)出現(xiàn)快捷方式時的標(biāo)題 shortcutDisabledMessage : 快捷圖標(biāo)禁用時的提示語
3.在AndroidManifest.xml文件中設(shè)置靜態(tài)快捷方式
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="net.devwiki.shortcuts"><applicationandroid: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>動態(tài)快捷方式
動態(tài)快捷方式即在代碼中添加,更新快捷圖標(biāo).
ShortcutManager提供了添加,移除,更新,禁用,啟動,獲取靜態(tài)快捷方式,獲取動態(tài)快捷方式,獲取固定在桌面的快捷方式等方法.
可在Java代碼中動態(tài)對圖標(biāo)快捷方式進(jìn)行操作.
添加快捷方式
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();} }項(xiàng)目代碼
項(xiàng)目代碼在此處:Shortcuts
總結(jié)
以上是生活随笔為你收集整理的Android7.1图标快捷方式(AppShortcuts)实现Demo的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 命令查看java的class字节码文件、
- 下一篇: Vue.2.0.5-模板语法
