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

cong15gong 7年前
   <p>今天给手上的Nexus6P手机升级系统至Android7.1,体验了一下新功能: <strong>App Shortcuts(图标快捷方式)</strong> ,如下图所示:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/16cebda68833bde45fcd27c5f08d7194.png"></p>    <p>如何实现这样的快捷方式呢?</p>    <p>官方给出的实现步骤: <a href="/misc/goto?guid=4959721696994475081" rel="nofollow,noindex">App Shortcuts | Android Developers</a></p>    <h2><strong>分类</strong></h2>    <p>图标快捷方式(App Shortcuts)分为两种:</p>    <ul>     <li> <p>Static shortcuts(静态快捷方式)</p> </li>     <li> <p>Dynamic shortcuts(动态快捷方式)</p> </li>    </ul>    <p>静态快捷方式是写在xml文件中,而动态快捷方式是在Java代码中编写.</p>    <h2><strong>实现</strong></h2>    <h3><strong>环境要求</strong></h3>    <ol>     <li> <p>只有Android7.1(25)及以上手机才能使用该功能</p> </li>     <li> <p>要求SDK版本为25及以上</p> </li>     <li> <p>最低兼容版本为23(因为动态快捷方式使用 <strong>Icon</strong> 类)</p> </li>    </ol>    <p>module 的build.gradle配置如下:</p>    <pre>  <code class="language-java">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'  }</code></pre>    <h3><strong>静态快捷方式</strong></h3>    <p>1.在项目的res/文件夹下建立 xml目录</p>    <p>2.在xml目录创建shortcuts.xml文件,内容如下</p>    <pre>  <code class="language-java"><?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></code></pre>    <p>属性含义和图片说明如下:</p>    <pre>  <code class="language-java">shortcutId : 快捷方式的ID  enabled : 是否可用,不可用时点击Toast提示shortcutDisabledMessage  icon : 快捷方式图标  shortcutShortLabel : 快捷图标放置到桌面时的标题  shortcutLongLabel : 长按图标出现快捷方式时的标题  shortcutDisabledMessage : 快捷图标禁用时的提示语</code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/ec9657210e9acada2c00d89d889e398c.png"></p>    <p>3.在AndroidManifest.xml文件中设置静态快捷方式</p>    <pre>  <code class="language-java"><?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></code></pre>    <h3><strong>动态快捷方式</strong></h3>    <p>动态快捷方式即在代码中添加,更新快捷图标.</p>    <p>ShortcutManager 提供了添加,移除,更新,禁用,启动,获取静态快捷方式,获取动态快捷方式,获取固定在桌面的快捷方式等方法.</p>    <p>可在Java代码中动态对图标快捷方式进行操作.</p>    <p><strong>添加快捷方式</strong></p>    <pre>  <code class="language-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();      }  }</code></pre>    <p><strong>移除快捷方式</strong></p>    <pre>  <code class="language-java">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();      }  }</code></pre>    <p> </p>    <p> </p>    <p>来自:http://blog.devwiki.net/index.php/2016/10/20/android-app-shortcuts-demo.html</p>    <p> </p>