
Shortcut 是Android-25(Android 7.1)新增的一项类似iOS的 3D Touch 功能的方便方法模块。可是有着不同的表现形式。由于Android在算法上不支持触摸压力感知,所以表现形式为长按,而iOS须用力长按。
首先。来个效果图
在 Launcher 或 应用程序列表 里面。长按使用页面安卓模仿3d touch模块,弹出一个快捷手段列表。 而且,能够把单个快捷手段拖动出来也是一个桌面按钮,拖出来的页面会随着清除使用数据或卸载使用而消逝,须又一次创建。
在主module下,改动 build.grade,使其使用 android-25 的 API 编译,当然,未下载的,就须要打开Android SDK Manager下载一下。
android {
compileSdkVersion 25
buildToolsVersion "25.0.0" // 或以上
defaultConfig {
targetSdkVersion 25
}
}

相似BroadCastReceiver,Shortcut注冊也分为静态注冊和动态注冊。首先介绍静态注冊安卓模仿3d touch模块,动态注冊后面继续~~
在 res/xml 目录下边创建一个xml。举个栗子:shortcut.xml
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@mipmap/ic_bar_detail_write"
android:shortcutDisabledMessage="@string/shortcut_publish"
android:shortcutId="publish"
android:shortcutLongLabel="@string/shortcut_publish"
android:shortcutShortLabel="@string/shortcut_publish">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.yanshi.writing.ui.bar.PublishPostActivity"
android:targetPackage="com.yanshi.writing" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
<shortcut
android:enabled="true"
android:icon="@mipmap/logo"
android:shortcutDisabledMessage="@string/shortcut_write"
android:shortcutId="write"
android:shortcutLongLabel="@string/shortcut_write"
android:shortcutShortLabel="@string/shortcut_write">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.yanshi.writing.ui.write.WriteActivity"
android:targetPackage="com.yanshi.writing" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
1、enabled:表示目前便利手段是否可使用
2、 icon: 快捷手段图标
3、 shortcutDisabledMessage: 快捷手段不可使用时表明的全名
4、 shortcutId:快捷手段标识

5、 shortcutLongLabel:长按下页面弹出来列表框中每一个快捷名
6、 shortcutShortLabel:快捷是希望单独显示在茶几上的,显示名为shortcutShortLabel
7、 targetClass:点击方便方法进入的Activity
8、categories 默认写死就可以
清单文件注冊
在 AndroidMainfest.xml 的默认启动页里加入 meta-data 标签配置
<activity
android:name=".ui.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoneTranslucent">
<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/shortcut" />
</activity>完成! 能够到茶几查看效果了~~

动态创建添加了菜谱配置的灵活性,比方能够从服务端拉取方便方法页面,再进行展示。详细配置原理例如以下:
在须要注冊的地方加入例如以下代码:
/**
* 动态创建
*/
public void register() {
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
List<ShortcutInfo> infos = new ArrayList<>();
// 按下返回button跳转的activity
Intent intent1 = new Intent(this, MainActivity.class);
intent1.setAction(Intent.ACTION_VIEW);
// 目标activity
Intent intent2 = new Intent(this, PublishPostActivity.class);
intent2.setAction("com.yuyh.xxx.BACK");
Intent[] intents = new Intent[2];
intents[0] = intent1;
intents[1] = intent2;
ShortcutInfo info = new ShortcutInfo.Builder(this, "publish-2")
.setShortLabel("动态创建-公布帖子")
.setLongLabel("动态创建-公布帖子")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_bar_detail_write))
.setIntents(intents)
.build();
infos.add(info);
mShortcutManager.setDynamicShortcuts(infos);
}
又一次执行app,再次长按,效果例如以下:
动态删除能够删除动态配置的方便方法。
/**
* 动态删除
*/
public void delete() {
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
/********* 移除弹出列表图标 **********/
// 全部动态创建图标
List<ShortcutInfo> infos1 = mShortcutManager.getDynamicShortcuts();
List<String> ids1 = new ArrayList<>();
for (ShortcutInfo info : infos1 ) {
ids1.add(info.getId());
}
// 禁用全部的快捷方式
mShortcutManager.disableShortcuts(ids1, "已禁用");
mShortcutManager.removeDynamicShortcuts(ids1);
/********* 移除拖出来的桌面快捷图标 **********/
// 放在桌面的图标
List<ShortcutInfo> infos2 = mShortcutManager.getPinnedShortcuts();
List<String> ids2 = new ArrayList<>();
for (ShortcutInfo info : infos2 ) {
ids2.add(info.getId());
}
mShortcutManager.disableShortcuts(ids2, "已禁用");
mShortcutManager.removeAllDynamicShortcuts();
}

代码比較简单。就不多做记叙了。 须提醒一下 getPinnedShortcuts 方法与 getDynamicShortcuts 方法的区别!
禁用后的作用如图所见到的,图标成为金色:
快捷手段的唯一性。由左边提到的 shortcutId 这个标识符决定,所以更新方便方法与创建快捷手段一样。 shortcutId 假设同样, 则会覆盖之前创建的方便方法!
当通过方便方法打开时。现有的Activity都会被销毁,然后又一次创建一个Activity栈。
由于清单形式设置的快捷键的Intent不能自己定义Intent的Flag,其默认的Flag是 FLAG_ACTIVITY_NEW_TASK 和 FLAG_ACTIVITY_CLEAR_TASK。
通过动态注冊的手段,可发现。我们希望配置返回目标activity。当然,静态配置也希望实现。改动shortcut标签:
<shortcut
android:enabled="true"
android:icon="@mipmap/ic_bar_detail_write"
android:shortcutDisabledMessage="@string/shortcut_publish"
android:shortcutId="publish"
android:shortcutLongLabel="@string/shortcut_publish"
android:shortcutShortLabel="@string/shortcut_publish">
<!-- 返回目标activity -->
<intent
android:action="com.yuyh.xxx.BACK"
android:targetClass="com.yanshi.writing.ui.MainActivity"
android:targetPackage="com.yanshi.writing" />
<!-- 目标activity -->
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.yanshi.writing.ui.bar.PublishPostActivity"
android:targetPackage="com.yanshi.writing" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
感谢阅读。
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/shouji/article-119697-1.html
现在的腌肉都是用化学物质腌制的