日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android7.1图标快捷方式(AppShortcuts)实现Demo

發布時間:2025/5/22 Android 166 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android7.1图标快捷方式(AppShortcuts)实现Demo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天給手上的Nexus6P手機升級系統至Android7.1,體驗了一下新功能:App Shortcuts(圖標快捷方式),如下圖所示:

如何實現這樣的快捷方式呢?

官方給出的實現步驟:App Shortcuts | Android Developers

分類

圖標快捷方式(App Shortcuts)分為兩種:

  • Static shortcuts(靜態快捷方式)

  • Dynamic shortcuts(動態快捷方式)

靜態快捷方式是寫在xml文件中,而動態快捷方式是在Java代碼中編寫.

實現

環境要求

  • 只有Android7.1(25)及以上手機才能使用該功能

  • 要求SDK版本為25及以上

  • 最低兼容版本為23(因為動態快捷方式使用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' }

    靜態快捷方式

    1.在項目的res/文件夾下建立 xml目錄

    2.在xml目錄創建shortcuts.xml文件,內容如下

    <?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 : 是否可用,不可用時點擊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"><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>

    動態快捷方式

    動態快捷方式即在代碼中添加,更新快捷圖標.

    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

    總結

    以上是生活随笔為你收集整理的Android7.1图标快捷方式(AppShortcuts)实现Demo的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。